我在div上包含ajax请求的结果并附加到页面中的主div。因此,如果结果数组返回为空,我想提醒用户,说没有找到结果,并提供链接供他们点击。当他们点击一个按类别携带产品的弹出框时会弹出。这里要注意的重要事项是,产品是通过php和ajax脚本派生的。
所以当我使用onclick事件或直接使用时,没有任何反应!但如果我调用空或HTML弹出它会工作。如果我在页面加载或ajax成功函数中触发它,弹出的相同php脚本也会起作用。所以我不明白为什么只有点击它不起作用?
这是我想在“点击”事件
上触发的弹出窗口 <section id="browse-search-popup" class="mfp-hide white-popup">
<h1>Browse Courses</h1>
<a href="">Can't find what you're looking for? Tell us here!</a>
<h2>First, pick a learning Zone:</h2>
<div class="row">
<div class="small-12 medium-8 large-8 medium-centered large-centered columns">
<ul class="small-block-grid-2 medium-block-grid-2 large-block-grid-2">
<?php
$tutor = new register();
$data = $tutor->get_maincat();
$i=0;
foreach($data as $k=>$v)
{
$i++;
?>
<li><div class="mainCat_<?php echo $i;?>" id="<?php echo $v['main_cat_id'];?>"><?php echo $v['main_cat_name'];?></div></li>
<?php
}
?>
</ul>
</div>
</div>
<h2>Then, pick a field:</h2>
<div class="row">
<div class="small-12 medium-12 large-12 columns" id="cat">
</div>
</div>
<h2>Finally, choose your Course:</h2>
<div class="row">
<div class="small-12 medium-12 large-12 columns" class="choosen_subjects">
</div>
</div>
<div class="row">
<div class="small-12 medium-12 large-12 columns" id="sub">
</div>
</div>
<input type="submit" id="done" value="Done">
</section>
Ajax请求
$("#search_tutor").submit(function() {
var data = {
"action": "test"
};
var subject = $("#subject").val();
data = $(this).serialize() + "&" + $.param(data);
$.ajax({
type: "POST",
dataType: "json",
url: "/fetch_tutor.php", //Relative or absolute path to response.php file
data: data,
success: function(data) {
$("#contents img.loading").remove();
var sizy = data.length;
if(sizy ==0) {
console.log("sorry, cannot locate");
$(".simply-popup-link").trigger("click");
$("#simply-popup").empty();
//This is where I want to trigger the on click
$("#simply-popup").append("Unable to locate the subject you entered, please <a href='#' onclick='browse("+sizy+")' class='yellow'>Browse</a>");
}
console.log("size= "+data.length);
var j=0;
for (i = 0; i < data.length; i++) {
........................
......................
浏览功能:
function browse(param) {
//this would work
/* $(".simply-popup-link").trigger("click");
$("#simply-popup").empty();
$("#simply-popup").append("test only"); */
// but not this
$(".browse-search-popup-link").trigger("click");
}
我尝试将页面加载到一个空的弹出框($(“#simply-popup”))中,但这样做不起作用:
$("#simply-popup").load('search/browse.php');
答案 0 :(得分:1)
更改
$("#simply-popup").append("Unable to locate the subject you entered, please <a href='#' onclick='browse("+sizy+")' class='yellow'>Browse</a>");
到
$("#simply-popup").append("Unable to locate the subject you entered, please <a href='#' class='yellow browse'>Browse</a>");
为此
编写单独的代码$('body').on('click', '.browse', function() {
$('.browse-search-popup-link').click();
});
答案 1 :(得分:0)
你不能在追加后调用该函数:
$("#simply-popup").append("Unable to locate the subject.....");
browse(sizy);// call it here.
或当您将其附加到元素中时,您可以调用点击:
$("#simply-popup").append("Unable to locate the subject.....");
$("#simply-popup a").click();
$(document.body).append('<a href="#" onclick="browse('+1+')">browse value is 1</a>');
$(document.body).find('a').click();
function browse(num){ $(document.body).find('p').html("<pre>"+num+"</pre>");}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
log out here:
<p></p>