【问题标题】:JQuery/Ajax form not submittingJQuery/Ajax 表单未提交
【发布时间】:2012-10-20 03:19:53
【问题描述】:

我在这个问题上大吃一惊——我有一个搜索 MySQL 数据库并在表格中返回结果的页面。我希望用户能够更新结果,从而更新 MySQL 数据库。第一部分工作正常,我在页面上有一个搜索框,它使用 jQuery/ajax 来查询数据库并显示结果,例如:

<form class="well" id="jquery-submit-ajax" method="post">
    <input type="text" name="userSearch" placeholder="Search…">
    <label>Name/Email/id</label>
    <Br />
    <input type="submit" id="searchButton" value="Search">
</form>

<div class="alert alert-success hide">
    <div id="success-output" class="prettyprint"></div>
</div>
<div id="loading-image" class="alert hide" style="text-align:center">
    <img src="../images/ajax-loader.gif" /></div>
    <div class="alert alert-error hide">
        <div id="error-output" class="prettyprint"></div>
    </div>
</div>

和 jQuery:

$("#jquery-submit-ajax").submit(function(e) {
    $('#loading-image').fadeIn();
    $.ajax({
        type: "POST",
        url: "jquery-ajax-control.php",
        data: $(e.target).serialize(),
        dataType: "html",
        beforeSend:function(){
            $('.alert-error,.alert-success').hide();
        },
        error: function(jqXHR, textStatus, errorThrown){
            $('#loading-image').hide();
            $('.alert-error').fadeIn();
            $('#error-output').html(errorThrown);
        },
        success: function(data){
            $('#loading-image').hide();
            $('.alert-success').fadeIn();
            $('#success-output').html(data);
        }
    });
    return false;
});

所以结果被解析成每个结果的表格。该表中有一个带有提交按钮的表单。例如:

<form method="post">
<table>
<tr>
    <td><input type="text" class="editbox" name="subs-expiry" value="<?php echo $expiry_date; ?>"</td>
</tr>
</table>
<input type="submit" class="updateSubsButtons" value="Update" />
</form>

我正在尝试再次通过 jQuery/Ajax 提交此表单,但无法正常工作。按下更新按钮会刷新整个页面。我已经把这个按钮的 jQuery 去掉了,只是在按下按钮时显示一个警报,但即使这样也不起作用。

$(".updateSubsButtons").on("submit", function(e){
    alert("clicked");
    e.preventDefault();
});

使用 Chrome 调试器,函数中的断点永远不会被命中,所以也许 jQuery 找不到按钮?我已经尝试过 .on() 和 .live() 函数,但我得到了相同的结果。我真的不知道这里发生了什么,所以任何帮助都将不胜感激!

【问题讨论】:

  • 您是否尝试过使用.on('click', ...) 而不是.on('submit', ...) 来阻止页面提交?如果输入是动态添加的,请尝试 $(document).on('submit', '.updateSubsButtons', function() {})$(document).on('click', '.updateSubsButtons', function() {})
  • 可能会失败,因为 $("#jquery-submit-ajax").submit( 在没有 preventDefault 的情况下运行,然后再转到 $(".updateSubsButtons").on (“提交”... ?

标签: php jquery ajax forms


【解决方案1】:

尝试将其替换为按钮和“点击”事件。

<button class="updateSubsButtons">Update</button>


$(".updateSubsButtons").on("click", function(e){
    alert("clicked");
    console.log(e);
    e.preventDefault();
});

这是处理异步加载项的方法(添加是因为这是实际解决问题的方式。):

$(".AlreadyLoadedParent").on("click",'.buttonYouWantToClick', function(e){
    alert("clicked");
    console.log(e);
    e.preventDefault();
});

【讨论】:

  • 这行得通,谢谢。虽然我不得不稍微改变它,就像你在下面的评论中说的那样,到 $(document).on('click', '.updateSubsButtons', function(e) {});
  • 只要确保您没有将所有内容都绑定到文档。不幸的是效率低下。除非文档是唯一不会被异步更改的东西。
  • 假设你有一个不会改变的内容 div、页脚 div 或侧边栏 div,但内容会:你想将点击附加到侧边栏,然后指定哪个您正在单击的按钮。这比附加到文档更有效。
【解决方案2】:

首先,您“单击”一个按钮,而不是“提交”它。您提交表格。所以你应该使用“点击”事件。

其次,如果应该触发 jQuery 代码的按钮是使用 AJAX 加载的,您应该使用 .live() 函数绑定事件,如下所示:

$('.updateSubsButtons').live('click', function(e) {
    e.preventDefault();
    // Your code here...
}

这样,即使在页面加载后,click 事件也会绑定到每个新对象,其 class="updateSubsButtons" 也会加载。如果您使用 $('.identifier').click() 它仅适用于页面加载时已加载的对象,而不适用于通过 AJAX 加载的元素。

【讨论】:

  • .live() 已被弃用。您应该使用 .on(),以按钮的父级为目标:$('buttons_parent').live('click', '.updateSubsButtons', function(e) {});
猜你喜欢
  • 2021-09-30
  • 2011-04-23
  • 2012-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多