单击添加值以形成数组 [英] add value to form array on click
问题描述
我在onclick上运行以下代码.
I have the below code working onclick.
<form action="" name="testform" method="post" id="testform">
<input type="hidden" value="" name="a">
<input type="hidden" value="" name="b">
<input type="hidden" value="" name="c">
<select>
<option value=""></option>
<option value="" onclick="document.testform.a.value='12'; document.testform.b.value='111'; document.testform.c.value='123'">1</option>
<option value="" onclick="document.testform.a.value='21'; document.testform.b.value='222'; document.testform.c.value='232'">2</option>
<option value="" onclick="document.testform.a.value='32'; document.testform.b.value='333'; document.testform.c.value='233'">3</option>
<option value="" onclick="document.testform.a.value='43'; document.testform.b.value='444'; document.testform.c.value='344'">4</option>
<option value="" onclick="document.testform.a.value='54'; document.testform.b.value='555'; document.testform.c.value='345'">5</option>
</select>
选择1时,a,b,c的隐藏字段的值为1.
When you select 1 the hidden fields of a,b,c get the value of 1.
这很好,但是我在这里有一个更大的项目,用户可以在整个表单中为"a"选择多个值.没有onclick,我可以很好地工作,例如
This works fine however I have a larger project in mind here where the user can select multiple values for 'a' throughout the form. Without onclick I can get this working well eg
<input type="hidden" name="a[]" value="1">
但是当我这样做
<form action="" name="testform" method="post" id="testform">
<input type="hidden" value="" name="a[]">
<input type="hidden" value="" name="b[]">
<input type="hidden" value="" name="c[]">
<select>
<option value=""></option>
<option value="" onclick="document.testform.a[].value='12'; document.testform.b[].value='111'; document.testform.c[].value='123'">1</option>
<option value="" onclick="document.testform.a[].value='21'; document.testform.b[].value='222'; document.testform.c[].value='232'">2</option>
<option value="" onclick="document.testform.a[].value='32'; document.testform.b[].value='333'; document.testform.c[].value='233'">3</option>
<option value="" onclick="document.testform.a[].value='43'; document.testform.b[].value='444'; document.testform.c[].value='344'">4</option>
<option value="" onclick="document.testform.a[].value='54'; document.testform.b[].value='555'; document.testform.c[].value='345'">5</option>
</select>
我没有任何东西插入我的隐藏值中.
I get nothing inserted into my hidden value.
这是为什么?
是否有更好的方法来完成我要使用jQuery的工作?
Is there a better way of doing what I am trying to do with jQuery?
推荐答案
这是针对您的问题的经过修改的jsfiddle链接. http://jsfiddle.net/et3r7/12/请检查是否对您有帮助.我对文本进行了隐藏,以显示结果,您可以将其隐藏.
Here is the modified jsfiddle link for your problem. http://jsfiddle.net/et3r7/12/ Please check if this helps you. I've made hidden to text in order to display the result you can make it them to hidden.
我已经稍微整理了一下您的代码.删除了所有的onClick代码.
I had cleaned up your code little bit. Removed all the onClick code.
这篇关于单击添加值以形成数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!