[英]How do I get input value and Selected Value of dynamically created inputs using JavaScript or JQuery in IE8 and IE9?
I have this working in IE 7 using getElementById() but in IE8 and IE9 that does not work. 我在使用getElementById()的IE 7中工作,但在IE8和IE9中不起作用。 What I am doing is I need to do validation on the select boxes to see that they are required and check what they selected to do other things.
我正在做的是我需要在选择框上进行验证,以查看它们是否是必需的,并检查他们选择执行的其他操作。 I also need to check the input boxes to see what the value is and it is required as well.
我还需要选中输入框以查看值是什么,以及它也是必需的。
function saveTest(){
var startNumber=0;
var endNumber=3;
// Works
alert(document.myForm.mySelect_1.value);
for (i = startNumber; i <= endNumber; i++) {
// I know this is not right but I am trying everything
alert(document.myForm.mySelect_+i.value);
// Using JQuery to try to solve the issue and this does not work for me.
alert($('#mySelect_'+i).val());
}
}
I know these are not dynamically created belwo but this is just to help me find the answer to this issue. 我知道这些不是动态创建的,但这只是为了帮助我找到此问题的答案。 I am creating more selects
我正在创建更多选择
<input name="myText_1" id="myText_1" value="">
<br />
<select name="mySelect_1" id="mySelect_1">
<option value="0">- - - Select One - - -</option>
<option value="1">item 1</option>
<option value="2">item 2</option>
<option value="3">item 3</option>
<option value="4">item 4</option>
<option value="5">item 5</option>
</select>
<br />
<input name="myText_2" id="myText_2" value="">
<br />
<select name="mySelect_2" id="mySelect_2">
<option value="0">- - - Select One - - -</option>
<option value="1">item 1</option>
<option value="2">item 2</option>
<option value="3">item 3</option>
<option value="4">item 4</option>
<option value="5">item 5</option>
</select>
<br />
<input name="myText_3" id="myText_3" value="">
<br />
<select name="mySelect_3" id="mySelect_3">
<option value="0">- - - Select One - - -</option>
<option value="1">item 1</option>
<option value="2">item 2</option>
<option value="3">item 3</option>
<option value="4">item 4</option>
<option value="5">item 5</option>
</select>
<br />
<a href="javascript:saveTest();">Save</a>
If I am not clear enough let me know. 如果我不够清楚,请告诉我。 I will do my best to explain anything you do not understand.
我会尽力向您解释您不理解的任何内容。
Thanks for your time. 谢谢你的时间。
The following line is not correct; 以下行是不正确的; you are trying to concatenate a variable name, called
mySelect_
to an integer: 您正在尝试将名为
mySelect_
的变量名称连接为整数:
alert(document.myForm.mySelect_+i.value);
Instead, you should use the associative array syntax: 相反,您应该使用关联数组语法:
alert(document.myForm['mySelect_'+i].value);
Also, within this same loop, you should start from 1
instead of 0
, since there's no element called mySelect_0
. 另外,在同一循环中,应该从
1
而不是0
,因为没有名为mySelect_0
的元素。
You have created drop downs with Names 您已使用名称创建下拉列表
Try using 尝试使用
$('select[name=mySelect_'+i+']').val();
else 其他
$("#id option:selected").text()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.