javascript - jquery如何发布到php,同步

标签 javascript php jquery post

我想post到php,当id EmpAgree1时,然后它的post变量EmpAgree=1;当id为EmpAgree2时,则后置变量EmpAgree=2等。但只是读取i的最后一个值,为什么?以及如何解决呢?

谢谢

$(document).ready(function() {
    var i;
    for(i=1; i <= <?php echo sizeof($id) ?> ; i++) {
        $("#EmpAgree"+i).click(function() {
            $.post("information_receiving.php", {
            EmpAgree : i
            });

            document.location.href="information_receiving.php";
        });
     }

最佳答案

您将使用 i 的最后一个值,在本例中为 (sizeof($id))。 要解决此问题,请尝试将数字存储在 #EmpAgree 元素中:

$(document).ready(function() {
var i;
for( i=1; i <= <?php echo sizeof($id) ?> ; i++){
 $("#EmpAgree"+i).data('nbr', i).click(function(){
     $.post("information_receiving.php",{
         EmpAgree : $(this).data('nbr')
});
   document.location.href="information_receiving.php";
});
}

关于javascript - jquery如何发布到php,同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38260148/

相关文章:

javascript - 标记动画刷新

javascript - 从 URL 将变量传递到简单模态弹出窗口

php - 使 :command not defined in Laravel 5. 1.7

jquery - 我怎样才能用 jquery 获得这种效果(在高度上)?

jquery - 如何使用jquery正则表达式在标签中查找电话号码

javascript - 当链接在 href 中有哈希时,链接仅在移动设备上切换 - jquery

javascript - 如何让prototype.js 模板编码HTML 实体?

javascript - jQuery 数据表 - 始终显示 X 条目中的 1 个

mysql - fatal error : Call to undefined function mysqli_init()

php - 为什么 "\"在将它存储在 PHP 的字符串中时出错?