简体   繁体   English

带有html / php表单(使用ajaxForm)的jQuery仅工作一次

[英]jQuery with html / php form (using ajaxForm) working only once

I've been trying to solve this for a while now and no success. 我已经尝试解决这个问题一段时间了,但没有成功。 Stackoverflow needed! 需要Stackoverflow!

Basically, what the script does is fetch user profile pictures from the database. 基本上,脚本的作用是从数据库中获取用户个人资料图片。 A moderator can see them and click on the "Ban" button, and then the banPeopleProcess.php file has the code to remove the picture from the database etc. Example: http://screencast.com/t/uFtFgKxYx4s 主持人可以看到它们,然后单击“禁令”按钮,然后banPeopleProcess.php文件具有从数据库中删除图片等的代码。示例: http ://screencast.com/t/uFtFgKxYx4s

Each picture is a form, they all have the same form id. 每张图片都是一个表单,它们都有相同的表单ID。 Here's the HTML (in php echo): 这是HTML(在php echo中):

<div>
<form id=\"banPeopleForm\" action=\"banPeopleProcess.php\" method=\"post\">
    <input type=\"hidden\" name=\"toid\" value=". $i ." />
    <input id=\"banPeopleBtn\" type=\"submit\" name=\"submit\" value=\"BAN\" />
</form>
</div>

The JS currently looks like this: JS当前看起来像这样:

<script type="text/javascript"> 

function banPeople() {
    $('#banPeopleForm').ajaxForm(function() {
        $('#banPeopleBtn').hide(); 
    });
}

$(document).ready(function() {
    $('#banPeopleBtn').click(function() {
        banPeople();
        });
    }); 
</script> 
  • Only the first loaded picture actually does what it should be doing (button is hiding, form submitted etc.) 实际上只有第一个加载的图片会执行其应做的工作(按钮处于隐藏状态,表单已提交等)
  • For the following pictures, the JS doesn't work, they just follow the normal HTML form - when clicking the ban button, we are redirected to banPeopleProcess.php. 对于以下图片,JS不起作用,它们仅遵循普通的HTML格式-单击“禁止”按钮时,我们将重定向到banPeopleProcess.php。
  • With that said it seems like only the first photo id gets loaded with the JS, and the others not. 话虽如此,看来只有第一个照片ID才装有JS,而其他照片没有。 I still don't know what to try. 我仍然不知道该怎么做。

Help greatly appreciated guys!! 帮助非常感谢的家伙!

You should prevent form from submitting php file and use ajax for calling your php.something like that 您应该阻止form提交php文件,并使用ajax调用php.something之类的东西

$(document).ready(function() {
    $('#banPeopleBtn').click(function(e) {
        e.preventDefault();
        banPeople();    
        $.ajax({
        type: "POST",
        url: "banPeopleProcess.php"
        });

        });
    }); 

Use different id for each picture. 每张图片使用不同的ID。 use different form id and submit id for each picture. 使用不同的表单ID并为每张图片提交ID。 Make dynamic the javascript id as well as followed $('#banPeopleBtn'+id).hide(); 使动态javascript ID以及后面的$('#banPeopleBtn'+ id).hide(); As javascript can not work with same id in same page. 由于javascript不能在同一页面中使用相同的ID。

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

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