0

我是 Jquery 的新手,我正在尝试附加一个选择选项。这有效,当我在选择中选择新添加的选项时。我在警报中变得不确定。

var selects = $(this).closest('div').find('.accSelectandlookup') ;

//Check if the accid is already present in the selectoptions and if not then add  

if (!selects.find('option[value="' + accid + '"]').length) {
alert(accid); // This throws up the correct value
selects.append('<option value="' + accid + '">Attach to Existing : ' + accname + '</option>');
}

我究竟做错了什么 ?

提前致谢

selects.change(function(){

alert('selected value is ' +  selects.filter(":selected").val()); // This throws undefined
})
4

2 回答 2

1

只需使用$(this).val() 即可获得当前的选定optionselect。由于范围可变,在触发事件selects时可能不可用。change

selects.change(function(){
    alert('selected value is ' + $(this).val()); // This throws undefined
});
于 2013-05-21T06:04:48.973 回答
1

当你加载这个.change()函数时,它会读取当前的 DOM。

当 you 时append,它正在更改 DOM,但这不会更新.change()正在查看的内容。所以你需要像这样修改它......

$('body').change(function(){
    alert('blah');
}, '.accSelectandlookup');

这保持.change()LIVE,使用当前的 DOM。

于 2013-05-21T06:08:44.333 回答