简体   繁体   English

为什么 JQuery 加载不发送我的变量?

[英]Why JQuery load doesn't send my variable?

I've got some code, which works fine, but I wanted to change it a little and the small problem has arisen.我有一些代码,它工作正常,但我想稍微改变它,小问题出现了。

    <button data-toggle='modal' data-target="#myModal" class='myClass' id='133'>Click me</button>

    $('.myClass').on('click', function(e){
        var idSend = $(this).attr('id');
        e.preventDefault();
    
    $('#myDiv').load( "action.php?action=doSomething", { id: idSend }, function() {
        alert(idSend);
    });
    $('#myModal').modal('show');
}

On the action.php page I make print_r($_POST);action.php页面上,我制作了print_r($_POST); and my variable is there.我的变量就在那里。 Now I wanted to use data-id instead of id attribute:现在我想使用 data-id 而不是 id 属性:

<button data-toggle='modal' data-target="#myModal" class='myClass' data-id='133'>Click me</button>

$('.myClass').on('click', function(e){
         var idSend = $(this).data('id');
         e.preventDefault();
    
          $('#myDiv').load( "action.php?action=doSomething", { id: idSend }, function() {
              alert(idSend);
          });
          $('#myModal').modal('show');
}

Function alert(idSend) shows my id number fine, but on the action.php page $_POST is empty.函数alert(idSend)显示我的 ID 号很好,但在 action.php 页面上 $_POST 是空的。 Can you please explain me this issue?你能解释一下这个问题吗?

I forgot, that my page is refreshing some content every 15 seconds.我忘了,我的页面每 15 秒刷新一次内容。 Thanks to @Quentin I realized that after one refreshing my second solution was working like first.感谢@Quentin,我意识到在一次刷新后,我的第二个解决方案就像第一个一样工作。 After that I found, that I have my first solution code duplicated in part of page, which was not being refreshed.在那之后,我发现我的第一个解决方案代码在页面的一部分中重复了,并且没有被刷新。 So my second solution was being overwritten by my first idea, but after refreshing it my second solution overwrote it and start to work again.所以我的第二个解决方案被我的第一个想法覆盖,但在刷新它后,我的第二个解决方案覆盖了它并重新开始工作。 Unfortunately in my duplicated code there was no alert yet, so I didn't know that it was happening.不幸的是,在我重复的代码中还没有alert ,所以我不知道它正在发生。

Ugh... so my messy code and sclerosis was the answer.呃......所以我的凌乱代码和硬化就是答案。

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

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