简体   繁体   English

Ajax只加载第一条记录吗?

[英]Ajax only loading first record?

This function works on loading an external page inside of a div that passes a $_POST variable and click works on the first record on loading the same external page with $_POST variable from query. 此函数可在通过$_POST变量的div内加载外部页面时起作用,并在使用查询中的$_POST变量加载同一外部页面时,单击第一条记录。

 $(document).ready(function(){
// load index page when the page loads
$("#form").load("tm-reserves-form.php");
$("#record").click(function(){
// load home page on click
    var ContactID = document.getElementById('ContactID').value;
    $("#form").load("tm-reserves-form.php", {"ContactID": ContactID});
    });
});

What is not functioning is this seems to only be passing the first record variable and no others. 没有起作用的是,这似乎仅传递了第一个记录变量,而没有传递其他任何变量。 I can click on the first record and the external page with data loads. 我可以单击第一条记录和带有数据加载的外部页面。 I click on other records and the div does not load with new data from click. 我单击其他记录,并且div不会加载来自click的新数据。 I have a page that creates a customer list from a mysql query, inside the record loop is a hidden input field with the primary key. 我有一个页面可以通过mysql查询创建客户列表,记录循环内部是带有主键的隐藏输入字段。 Any thoughts on what I am missing? 对我想念的东西有什么想法吗? I thought maybe to unbind click, but that did not work either. 我以为可能取消绑定点击,但这也不起作用。

HTML Code HTML代码

<div id="record"><?php do { ?><input type="hidden" name="ContactID" id="ContactID" value="<?php echo $row_tm_reserves['ContactID']; ?>"><!-- Other strings --><?php } while ($row_tm_reserves = mysql_fetch_assoc($tm_reserves)); ?></div>  

Help! 救命! and Thanks in advance! 并预先感谢!

This is the problem with event delegation , change 这是event delegation ,更改的问题

$("#record").click(function(){

});

to

$(document).on("click","#record",function(){

});

Mainly make sure you have only one id as record in your page. 主要确保您的页面中只有一个id作为record

您应该发布html代码,以帮助我们为您提供帮助...如果添加了新的dom元素,则无法在其上附加操作的脚本,因为在该脚本加载时它们不存在。

$(document).on("action","element",function()

The problem is that #form content gets replaced on every load . 问题在于, #form内容在每次load都会被替换

My solution: 我的解决方案:

You can create a new element inside #form , for example a <p> , and append to it, ie : 您可以在#form内创建一个新元素,例如<p> ,并append到该元素上,即:

$(document).ready(function(){
$("#form").load("tm-reserves-form.php");
$(document).on("click","#record",function(){
    var ContactID = document.getElementById('ContactID').value;
    $("#form").append($(document.createElement("p")).load("tm-reserves-form.php", {"ContactID": ContactID}));
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM