dqrsceg6279196 2012-06-12 02:06
浏览 59
已采纳

Javascript onclick将第二个输入框的值更改为与第一个输入框的值相同

I am creating a form using php. One of my field I created a six rows of drop down box with the same two option 'Floating Rate' and 'Fixed Rate' to select and also a button at the side of each drop down box. I want to make it easier for the user to select. For example if the user select 'Floating Rate' on the first row and click on the button at the side, the rest of the rows will change its selection to 'Floating Rate'. And if the user select either options on the 4th row, the 5th and the 6th row will change according to the option the user choose on the 4th row. I'm a noobie to Java or Jquery. Is there a way to achieve the above result using Javascript?

1st Row:

<td>
    <select class="dropDown" style="width:150px" name="rate_value_1">
    <option value="<?php echo $_SESSION['rate_value_1'] ;?>"><?php echo $_SESSION['rate_value_1'] ;?></option>
    <option value="Floating Rate">Floating Rate</option>
    <option value="Fixed Rate">Fixed Rate</option>
    </select>
</td>
<td><input type="button" value="equal" class="equal" /></td>

2nd Row:

<td>
    <select class="dropDown" style="width:150px" name="rate_value_2">
    <option value="<?php echo $_SESSION['rate_value_2'] ;?>"><?php echo $_SESSION['rate_value_2'] ;?></option>
    <option value="Floating Rate">Floating Rate</option>
    <option value="Fixed Rate">Fixed Rate</option>
    </select>
</td>
<td><input type="button" value="equal" class="equal" /></td>
  • 写回答

1条回答

  • dongpan1871 2012-06-12 02:30
    关注

    Here is the code:

    I simulated your situation like this:

    <table id="interest_rate">
    <tr>
    <td>
        <select class="dropDown" style="width:150px" name="rate_value_1">
        <option value="rate_value_1">rate_value_1</option>
        <option value="Floating Rate">Floating Rate</option>
        <option value="Fixed Rate">Fixed Rate</option>
        </select>
    </td>
    <td><input type="button" value="equal" class="equal" /></td>
    </tr>
    <tr>
    <td>
        <select class="dropDown" style="width:150px" name="rate_value_2">
        <option value="rate_value_2">rate_value_2</option>
        <option value="Floating Rate">Floating Rate</option>
        <option value="Fixed Rate">Fixed Rate</option>
        </select>
    </td>
    <td><input type="button" value="equal" class="equal" /></td>
    </tr>
    </table>
    

    And here is the javascript code that are going to work in that situation, you just need to put this code after the table.

    document.getElementById('interest_rate').onclick = function (e) {
      if (!e) {
        e = event;
      }
    
      // Get the clicked target
      var target = e.target || e.srcElement;
    
      // If this is the target that we want to get.
      if ('equal' == target.className && 'button' == target.type) {
        // Get the selected index of the 'SELECT' element.
        var defaultValue = target.parentNode.parentNode.getElementsByTagName('SELECT')[0].selectedIndex;
    
        // Loop and change all 'SELECT' element to the same selected index.
        for (var i = 0, selects = this.getElementsByTagName('SELECT'), selectsLength = selects.length; i < selectsLength; ++i) {
          selects[i].selectedIndex = defaultValue;
        }
      }
    };
    

    Live jsFiddle Demo


    EDIT1

    To change only the SELECT elements below the row, you need to identify the index of the current SELECT element and then change all below. Here is the code:

    document.getElementById('interest_rate').onclick = function (e) {
      if (!e) {
        e = event;
      }
    
      var target = e.target || e.srcElement;
    
      if ('equal' == target.className && 'button' == target.type) {
        var targetSelect = target.parentNode.parentNode.getElementsByTagName('SELECT')[0];
        if (targetSelect) {
          var defaultValue = targetSelect.selectedIndex;
    
          for (var i = 0, selects = this.getElementsByTagName('SELECT'), selectsLength = selects.length, status = 0; i < selectsLength; ++i) {
            if (status || selects[i] === targetSelect) {
              status = 1;
              selects[i].selectedIndex = defaultValue;
            }
          }
        }
      }
    };
    

    Live jsFiddle Demo


    EDIT2

    To change the value of INPUT elements with type="text", you just need to change selectedIndex to value and then check the type of INPUT element like this:

    document.getElementById('interest_rate').onclick = function (e) {
      if (!e) {
        e = event;
      }
    
      var target = e.target || e.srcElement;
    
      if ('equal' == target.className && 'button' == target.type) {
        var targetInput = target.parentNode.parentNode.getElementsByTagName('INPUT')[0];
        if (targetInput && 'text' == targetInput.type) {
          var defaultValue = targetInput.value;
    
          for (var i = 0, inputs = this.getElementsByTagName('INPUT'), inputsLength = inputs.length, status = 0; i < inputsLength; ++i) {
            if (status || inputs[i] === targetInput) {
              status = 1;
              if ('text' == inputs[i].type) {
                inputs[i].value = defaultValue;
              }
            }
          }
        }
      }
    };
    

    Live jsFiddle Demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛