【发布时间】:2012-03-14 19:41:23
【问题描述】:
我已经将以下内容剥离了,我正在传递更多属性以在我的 ajax 中调用特定信息。
$(document).on("click", ".call", function(){
$.ajax({type: "POST",url: "showform.asp",dataType:"html",data: {},cache: false,success:function(result){
$("#LoadArea").html(result);
}});
});
$(document).on("click", ".cbcall", function(){
$.ajax({type: "POST",url: "select.asp",dataType:"html",data: {},cache: false,success:function(result){
$("#LoadArea").html(result);
}});
});
上面是我正在使用的 Jquery 的示例,下面是我正在使用的 html。
<p class="call">click here to load</p>
<div id="LoadArea"></div>
当单击此处加载被单击时,showform.asp 页面内容被加载到 LoadArea div 中。 showform.asp的内容如下
<div class="call">
<input type="checkbox" class="cbcall">
</div>
问题是,如果我单击复选框(.cbcall),事件会冒泡并加载 showform.asp。我对 Jquery 还很陌生,所以我想知道如何停止事件冒泡,但仍然能够选中和取消选中 showform.asp 中加载的复选框。
我试过 return false;但这不会选中复选框。
谁能指出我正确的方向?
更新 - 调整代码以匹配建议
我已经更新了我的起始页以匹配 Nicola 的代码 (thanx Nicola)
$(document).ready(function(){
$(".call").on("click", function(e){
//check that event is originated by a <p class="call"> element
if($(e.target).is('.call')){
alert('.call actioned');
$.ajax({type: "POST",url: "showform.asp",dataType:"html",data: {},cache: false,success:function(result){$("#LoadArea").html(result);}});
}
});
$( ".call").on("click", ".cbcall", function(e){
alert('radio actioned');
//Stop immediate propagation
e.stopImmediatePropagation();
$.ajax({type: "POST",url: "select.asp",dataType:"html",data: {},cache: false,success:function(result){$("#LoadArea").html(result);}});
});
});
<p class="call">click here to load</p>
<div id="LoadArea"></div>
<!-- additonal code below here -->
<div class="call">
<input type="checkbox" class="cbcall">
</div>
另外我在页面底部添加了showform.asp的内容,并在Jquery中添加了一些警报。
当我在初始页面功能上单击<p class="call">click here to load</p> 或<input type="checkbox" class="cbcall"> 时,会触发警报并加载ajax,但是当我只使用<p class="call">click here to load</p> 并尝试从加载ajax 的showform 中执行相同的调用时.asp 页面没有任何反应。
【问题讨论】: