jquery 中没有触发事件,什么问题?
我有四个
<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>
子元素
$('#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 »</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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用 .live()。
从文档中:
You should use .live().
From the documentation:
您需要使用
delegate()
将更改处理程序添加到动态创建的元素:这是必要的,因为您现有的代码仅运行一次(大概是在加载文档时)并且仅绑定事件处理程序到当时存在的元素。委托将绑定事件所有现有元素,以及加上将来创建的所有元素。有关详细信息,请参阅 jQuery 委托文档。
如果您愿意,可以使用
bind()
代替。您可以将对bind()
的调用放在 AJAX 调用的成功函数中,以便在创建元素后将事件处理程序附加到该元素。编辑
有关
delegate()
和live()
之间差异的详细分析,请参阅jQuery:live() 与 delegate()You need to use
delegate()
to add the change handler to dynamically-created elements: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 tobind()
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()
andlive()
, see jQuery: live() vs delegate()