我创建了一个jQuery模型。
我试图在该模型内使用AJAX填充数据。
我获取了一个ID,并想使用AJAX来填充数据。
我应该如何在单击事件上调用AJAX?
当模型打开或加载时,是否有其他事件?
该模型只是显示和隐藏div。
只需使用:
JS:
$(document).ready(function(){
$('a.pop').click(function() {
var popID = $(this).attr('rel');
$.get('content.php', { ref:popID }, function(data) {
$(popID+'Container').html(data);
$(popID).dialog();
alert('Load was performed.');
});
return false; // prevent default
});
});
HTML:
<div id="example" class="flora" title="This is my title">
I'm in a dialog!
<div id="exampleContainer"></div>
</div>
<a href="#" id="clickingEvent" class="pop" rel="example">click to launch</a>
尽管我没有测试过,但在我看来,它应该可以工作...
你快要做到了,你需要阻止默认的操作,这个默认操作是跟随链接中的 href
,所以请添加 event.preventDefault()
或者 return false
,像这样:
$('a.pop').click(function(e) { //add e param
var popID = $(this).attr('rel'),
popURL = $(this).attr('href');
$.get("content.php", { ref:id}, function(data) { //did you mean popID here?
alert("Data Loaded: "+data );
});
e.preventDefault(); //or return false; //prevent default action
});
$.get("content.php", { ref:id},function(data){ alert("数据已加载: "+data ); } );
我想在这次点击事件中调用那个ajax文件。如何操作? - zod