Jquery .change() 函数不适用于动态填充的 SELECT 列表 [英] Jquery .change() function not working with dynamically populated SELECT list

查看:33
本文介绍了Jquery .change() 函数不适用于动态填充的 SELECT 列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个选择字段,该字段使用 ajax 调用动态填充,该调用仅返回所有 HTML 选择选项.这是 PHP 的一部分,它只是回显选择标签并动态填写每个选项/值.

I have a select field that is dynamically populated with an ajax call that simply returns all the HTML select options. Here is the part of the PHP that just echo's the select tags and dynamically fills out each option/value.

echo "<select name='player1' class='affector'>";
echo "<option value='' selected>--".sizeof($start)." PLAYERS LOADED--</option>";

foreach ($start as $value) {
    echo "<option value='".$value."'>".$value."</option>";
 }  
  echo "</select>";
}  

填充此列表后,我尝试调用更改事件,以便每当 SELECT 列表或具有相同类的文本字段中的默认选项发生更改时,它会禁用另一部分中设置的单选按钮的形式.(您可以看到我要求使这部分功能正常工作的最初问题 此处)

After this list is populated, I'm trying to call a change event, so that whenever the default option is changed in the SELECT list or in a text field with the same class, it disables a radio button set in another part of the form. (You can see the initial question I asked to get this part of the functionality working here)

$('.affector').change(function(){
       $(this).parents('tr').find('input:radio').attr('disabled', false);
});

出于某种原因,即使我为选择字段指定了正确的类名(影响器),当我在该字段中选择不同的选项时,表单的其他部分也不会禁用.具有相同类的静态文本字段工作正常.我被难住了.

For some reason, even though I give the select field the proper class name (affector), when I select different options in the field, the other parts of the form do not disable. The static text field with the same class works fine. I'm stumped.

有什么想法吗?

推荐答案

刚刚评论了你的最后一个问题...这是我说的:

Just commented on your last question...Here's what I said:

使用jQuery绑定

function addSelectChange() {
   $('select').bind('change', function() {
       // yada yada
   });
} 

在您的 ajax 成功函数中调用 addSelectChange.它应该可以解决问题.也看看 jQuery live event(当你想在以后的项目中为所有当前和未来的 DOM 元素设置事件时).不幸的是,live 尚不支持更改事件和其他一些事件.绑定是下一个最好的事情

Call addSelectChange in your ajax success function. It should do the trick. Take a look at jQuery live event too (for when you want to set events for all current and future DOM elements in a later project or something). Unfortunately the change event and a few others aren't yet supported with live. Bind is the next best thing

这篇关于Jquery .change() 函数不适用于动态填充的 SELECT 列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆