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

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

问题描述

我有一个select字段,该字段由ajax调用动态填充,该ajax调用仅返回所有HTML select选项.这是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);
});

由于某些原因,即使我为select字段指定了正确的类名(affector),当我在该字段中选择其他选项时,表单的其他部分也不会被禁用.具有相同类的静态文本字段可以正常工作.我很困惑.

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实时事件(例如,当您想为以后项目中的所有当前和将来DOM元素设置事件时).不幸的是,变更活动和其他一些活动尚未获得直播支持.绑定是下一个最好的东西

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天全站免登陆