开发者

Send same value to two hidden fields onchange

I'm having difficulty passing the same value to two hidden fields. So for example, if I select Product 1, I want both hidden fields to have a value of X.

<select>
<option value="X">Product 1</option>
<option value="Y">Product 2</option>
<option value="Z">Product 3</option>
</select>
<input id="test" name="product_id[]" type="hidden" value="">
<input id="test" name="product_id" type="hidden" value="">

I've tried using onchange methods, but I can only pass a single value only one of the hidden fields, not both.

Here is what I tried, granted I was using the same id for both fields which I realize now won't work.

<form action="../index.php" method="post" >     
 <div id="Select">
          <select id="myselect" onchange="this.form.prodhid.value=this.selectedIndex">
    <option>Please Select an Amount开发者_C百科</option>
             <option value="1">Product 1</option>
            <option value="2">Product 2</option>
            <option value="3">Product 3</option>
    </select>    
     <input type="hidden" name="product_id[]" id="prodhid" value="" />
     <input type="hidden" name="product_id" id="prodhid" value="" />
<input value="Add to Cart" title="Add to Cart" type="submit">
</form>


You can't access two DOM elements with the same id. So you may change your markup to something like this

<input id="test1" name="product_id[]" type="hidden" value="">
<input id="test2" name="product_id" type="hidden" value="">

Or you can try something like this:

<html>
  <head>
    <script type="text/javascript">
      function change()
      {
        var list = document.getElementById('test-group')
                           .getElementsByTagName('INPUT');
        for (var i = 0; i < list.length; ++i)
          list[i].value = 'X';
      }
    </script>
  </head>
  <body>    
    <select onchange="change()">
      <option value="X">Product 1</option>
      <option value="Y">Product 2</option>
      <option value="Z">Product 3</option>
    </select>
    <div id="test-group">
      <input name="product_id[]" type="hidden" value="">
      <input name="product_id" type="hidden" value="">
    </div>
  </body>
</html>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜