简体   繁体   English

如何在IE8和IE9中使用JavaScript或JQuery获取输入值和动态创建的输入的选定值?

[英]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的元素。

Here's a working DEMO . 这是一个正常工作的DEMO

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.

相关问题 在IE8中获取具有特定值的输入元素? - Get an input element with specific value in IE8? javascript / jQuery:在标题中添加换行符—在IE9中有效,但在IE8中无效 - javascript/jQuery: Adding linebreak in title — works in IE9, but not in IE8 无法让jQuery加载到IE8或IE9中 - Can't get jQuery to load in IE8 or IE9 如何使用javascript / jQuery将值分配给动态创建的输入框 - How to assign value to dynamically created input box using javascript/Jquery 当尝试通过innerHTML使用JavaScript动态添加表格元素时,不会在IE7,IE8和IE9中呈现 - When trying to dynamically add table elements using JavaScript via innerHTML doesn't render in IE7, IE8 and IE9 JavaScript可在FF + IE9中运行,但不能在IE8中运行 - Javascript Working in FF + IE9 but not IE8 IE8和IE9上的Javascript错误 - Javascript error on IE8 and IE9 如何使用javascript插入和获取动态创建的输入字段的值? - How to insert and get the value of a dynamically created input field using javascript? 如何检测我的应用程序是否在JavaScript中的IE8和IE9下运行? - How can I detect if my app runs under IE8 vs IE9 in JavaScript? Javascript在使用IE9兼容模式的IE8中有效,但在IE8安装中无效 - Javascript Works in IE8 Using IE9 Compatibility Mode but Not with IE8 Installation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM