jquery 中没有触发事件,什么问题?

发布于 2024-11-27 02:55:51 字数 2467 浏览 0 评论 0原文

我有四个

<div id="select-countries-container">
        <select name="countryId" id="select-countries">
            <option value="">Choose Country &raquo;</option>
        <?php foreach($location->fetch(array('table' => 'countries')) as $country) { ?>
            <option value="<?php echo $country['id']; ?>"><?php echo $country['name']; ?></option>
        <?php } ?>
    </select>
</div>
<div id="select-states-container"><select disabled="disabled"><option value="">Choose State &raquo;</option></select></div>
<div id="select-cities-container"><select disabled="disabled"><option value="">Choose City &raquo;</option></select></div>
<div id="select-areas-container"><select disabled="disabled"><option value="">Choose Area &raquo;</option></select></div>

子元素

$('#select-countries').change(function(){
    var countryId = $(this).val();
    if(countryId == ''){
        $('#select-states-container').html(chooseState);
    } else {
        $.ajax({
            type: 'POST',
            url:  'process.php',
            data: 'countryId='+countryId,
            beforeSend: function() {
                $('#select-states-container').html(loadingText);
            },
            success: function(states){
                if(states == 'null') {
                    $('#select-states-container').html(emptyState);
                } else {
                    $('#select-states-container').html(states);
                }   
            }
        });
    }
});

从 process.php 检索的数据是。

$select  = '<select name="stateId" id="select-states">';
$select .= '<option value = "">Choose State &raquo;</option>';
foreach($states as $state) {
    $select .= '<option value = "'.$state['id'].'">'.$state['name'].'</option>';
}
$select .= '</select>';
echo $select;

直到这里它工作得很好,当我从第二个 从 process.php 检索,当我从中选择值时,它不会触发它绑定到的函数,我不确定发生了什么,这就是我尝试做的。

$('#select-states').change(function(){
        alert('Fire');
});

这不会火。我的代码有什么问题吗?

谢谢。

I have four <select> element.

<div id="select-countries-container">
        <select name="countryId" id="select-countries">
            <option value="">Choose Country »</option>
        <?php foreach($location->fetch(array('table' => 'countries')) as $country) { ?>
            <option value="<?php echo $country['id']; ?>"><?php echo $country['name']; ?></option>
        <?php } ?>
    </select>
</div>
<div id="select-states-container"><select disabled="disabled"><option value="">Choose State »</option></select></div>
<div id="select-cities-container"><select disabled="disabled"><option value="">Choose City »</option></select></div>
<div id="select-areas-container"><select disabled="disabled"><option value="">Choose Area »</option></select></div>

the child element <select> is replaced with the code received from Ajax, here is the Ajax Code i am using.

$('#select-countries').change(function(){
    var countryId = $(this).val();
    if(countryId == ''){
        $('#select-states-container').html(chooseState);
    } else {
        $.ajax({
            type: 'POST',
            url:  'process.php',
            data: 'countryId='+countryId,
            beforeSend: function() {
                $('#select-states-container').html(loadingText);
            },
            success: function(states){
                if(states == 'null') {
                    $('#select-states-container').html(emptyState);
                } else {
                    $('#select-states-container').html(states);
                }   
            }
        });
    }
});

and the data being retrieved from process.php is.

$select  = '<select name="stateId" id="select-states">';
$select .= '<option value = "">Choose State »</option>';
foreach($states as $state) {
    $select .= '<option value = "'.$state['id'].'">'.$state['name'].'</option>';
}
$select .= '</select>';
echo $select;

till here it works perfectly fine, the trouble starts when i select states from second <select> i.e <select name="stateId" id="select-states"> retrieved from process.php, when i select the value from this it does not trigger the function it is bound to, i am not sure what is happening, here is what i tried to do.

$('#select-states').change(function(){
        alert('Fire');
});

and this does not fire. what is wrong with my code?

thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

走过海棠暮 2024-12-04 02:55:51

您应该使用 .live()

从文档中:

描述:将处理程序附加到所有元素的事件
匹配当前选择器,现在和将来。

You should use .live().

From the documentation:

Description: Attach a handler to the event for all elements which
match the current selector, now and in the future.

葬﹪忆之殇 2024-12-04 02:55:51

您需要使用 delegate() 将更改处理程序添加到动态创建的元素:

$("body").delegate("#select-states", "change", function(){
    ...

这是必要的,因为您现有的代码仅运行一次(大概是在加载文档时)并且仅绑定事件处理程序到当时存在的元素。委托将绑定事件所有现有元素,以及加上将来创建的所有元素。有关详细信息,请参阅 jQuery 委托文档

如果您愿意,可以使用 bind() 代替。您可以将对 bind() 的调用放在 AJAX 调用的成功函数中,以便在创建元素后将事件处理程序附加到该元素。

编辑

有关delegate()live()之间差异的详细分析,请参阅jQuery:live() 与 delegate()

You need to use delegate() to add the change handler to dynamically-created elements:

$("body").delegate("#select-states", "change", function(){
    ...

This is necessary because your existing code runs only once ( when the document is loaded, presumably) and binds the event handler only to the elements that exist at that time. Delegate will bind the event all existing elements, plus all elements that are created in the future. For more information, please see the jQuery delegate documentation.

If you wish, you can use bind() instead. You would place the call to bind() in the AJAX call's success function to attach the event handler to the element after it is created.

EDIT

For a good analysis on the differences between delegate() and live(), see jQuery: live() vs delegate()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文