onchange 时将相同的值发送到两个隐藏字段
我很难将相同的值传递给两个隐藏字段。例如,如果我选择“产品 1”,我希望两个隐藏字段的值均为 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="">
我已尝试使用 onchange 方法,但我只能向其中一个隐藏字段传递单个值,而不能同时传递两个值。
这是我尝试过的,假设我在两个字段中使用相同的 id,但我现在意识到这是行不通的。
<form action="../index.php" method="post" >
<div id="Select">
<select id="myselect" onchange="this.form.prodhid.value=this.selectedIndex">
<option>Please Select an Amount</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>
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</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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法访问具有相同 id 的两个 DOM 元素。所以你可以将你的标记更改为这样的东西
或者你可以尝试这样的东西:
You can't access two DOM elements with the same id. So you may change your markup to something like this
Or you can try something like this: