简体   繁体   English

jQuery HTML操作不适用于选择输入

[英]jquery html manipulation does not work on the select inputs

Please refer to http://jsfiddle.net/7FfMd/6/ 请参考http://jsfiddle.net/7FfMd/6/

run the code, then select "E" from the drop down, another input box appear, however I cannot select option B from the second input. 运行代码,然后从下拉列表中选择“ E”,出现另一个输入框,但是我无法从第二个输入中选择选项B。

How can I fix it. 我该如何解决。

For some reason, I when I select option "D" from the first input again, the second select box does not update too. 由于某种原因,当我再次从第一个输入中选择选项“ D”时,第二个选择框也不会更新。

Thanks 谢谢

That's because you catch the change event on the div that surrounds both dropdowns. 这是因为您在两个下拉列表周围的div上捕获了change事件。 When you change the second dropdown, the event handler will be triggered and remove the dropdown where you made the selection and replace it with a new dropdown. 当您更改第二个下拉菜单时,将触发事件处理程序,并从您选择的地方删除该下拉菜单,然后将其替换为新的下拉菜单。

If you want to bind the change event to the parent element, you need to check in which dropdown the change was made (using event.target ), so that you don't recreate all the dropdowns. 如果要将更改事件绑定到父元素,则需要检查在哪个下拉列表中进行了更改(使用event.target ),以免重新创建所有下拉列表。 If you use delegate to bind the event handler, this will contain the element where the change was made: 如果使用delegate来绑定事件处理程序, this它将包含进行更改的元素:

jQuery('.change').delegate('select', 'change', function() { ... });

It might be simpler to bind the event for each select, then you don't have to check wich dropdown that caused the event. 为每个选择绑定事件可能更简单,因此您不必检查导致事件的下拉列表。 If you use delegate , you can bind the event eventhough the element doesn't exist yet, but you need to put a class, name or id on the dropdowns so that they can be identified: 如果您使用delegate ,即使该元素尚不存在,您也可以绑定该事件,但是您需要在下拉菜单中放置一个类,名称或ID,以便可以识别它们:

jQuery('.change').delegate('#firstDropdown', 'change', function() { ... });
jQuery('.change').delegate('#secondDropdown', 'change', function() { ... });

You were not getting the val of the input, but were getting the val of the div containing the input: 您没有得到输入的val ,但是得到了包含输入的div的val

var testVar= jQuery('#placeholder2').val();

Should be 应该

var testVar = jQuery('#placeholder2 select').val();

Also, the change subscriber should only be on the select, not the div also (to avoid the event firing when the second select is changed): 同样, change订阅者应仅处于选择状态,而不是div(以避免更改第二个select时触发事件):

jQuery('.change').change(function() {

Should be 应该

jQuery('.change select').change(function() {

See here for updated version. 有关更新版本, 请参见此处

The problem is that you've bound the change event handler to the <div> element that contains both <select> elements. 问题是您已将change事件处理程序绑定到包含两个<select>元素的<div> <select>元素。 Whenever you change any of those <select> elements, the change event bubbles up to the <div> . 每当您更改这些<select>元素中的任何一个时,change事件就会冒泡到<div> That works fine for the first one, but when you change the second it's firing that code again and replacing the entire <select> element. 这对于第一个来说效果很好,但是当您更改第二个效果时,它将再次触发该代码并替换整个<select>元素。

That's because you're accessing the selected value with 那是因为您正在使用

var testVar= jQuery('#placeholder2').val();

This tries to get .val() from the div, not the select. 这会尝试从div中获取.val() ,而不是从select中获取。 Try this: 尝试这个:

var testVar= jQuery('#placeholder2 select').val();

Don't forget that when you add new elements, your previously applied methods won't have been applied to them. 不要忘记,当您添加新元素时,以前应用的方法将不会被应用到它们。

You will need to apply the event code to the new elements. 您将需要将事件代码应用于新元素。

You are creating a div that have the class .change. 您正在创建一个具有.change类的div。 So the function are handled by all change by its children. 因此,功能由其子项的所有更改处理。

Have a look at this code: 看一下这段代码:

http://jsfiddle.net/7FfMd/13/ http://jsfiddle.net/7FfMd/13/

your jquery was wrong. 您的jquery错误。 here is my answer Markup is as below 这是我的答案标记如下

<div class='change'>
    <div id="placeholder2" style="width:600px;">
        <select>
            <option selected="selected">D</option>
        <option >E</option>
        </select>
    </div>
    <div id="placeholder" style="width:600px;"></div>
</div>

jQuery should be like this. jQuery应该是这样的。

jQuery('#placeholder2>select').change(function(){   
    var testVar= $(this).val();
    alert(testVar);
    if (testVar == "D")
    {
        var htmlString = '<select><option>C</option></select>';
        jQuery('#placeholder').html(htmlString);
    } else
    {
        var htmlString = '<select><option>A</option><option>B</option></select>';
        jQuery('#placeholder').html(htmlString);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM