简体   繁体   English

如果没有页面刷新,Ajax表单将不会重新提交

[英]ajax form won't re-submit without page refresh

having a bit of a mystery issue here... 这里有点神秘的问题...

I've built a button on a page which submits a form. 我在提交表单的页面上建立了一个按钮。 The form is embedded with a DIV, which updates to show the amended data. 该表单嵌入了DIV,该DIV将更新以显示修改后的数据。

(it's within a smarty template) (位于智能模板中)

form: 形成:

<form id='itempost_{$item.itemid}_{$item2.itemid}' method='post' class='itempost'>
     <input type='hidden' name='item' value='{$item.itemid}'>
     <input class='btn_text' type="submit" value="Send Item">
</form>

JS JS

$('.itempost').on('submit', function(e) {

    e.preventDefault();
    $.ajax({
        type: "POST",
        url : 'item.php',
        data: $(this).serialize(),
        success : function (response) {
        $("#update").eq(0).load("update.php?&t={$parentid}");

        },
        error: function (jXHR, textStatus, errorThrown) {
        alert('not ok');
            alert(errorThrown);
        }
    });
    return false;
});

update.php update.php

all update.php contains is the necessary minimums to rebuild the page, then it rebuilds the section of template that sits within div #update. update.php包含的所有内容都是重建页面所需的最低要求,然后它将重建位于div #update中的模板部分。

$smarty->display('items.tpl');

The first time the button is clicked, it works brilliantly, and the HTML content changes without any noticeable impact on the page. 第一次单击该按钮,效果很好,并且HTML内容发生更改,而对页面没有任何明显影响。

The second click, however, doesn't seem to go anywhere near the JS, and simply reloads the page. 但是,第二次点击似乎没有到达JS的任何地方,只是重新加载了页面。 The following click then works as it should, leading me to believe the the actions are: 接下来的单击将按预期方式工作,使我相信这些操作是:

1st Click: Submits form with AJAX as designed 2nd Click: Refreshes the page, does nothing more 3rd Click: Submits form with AJAX as designed 4th Click: Refreshes the page, does nothing more 第一次单击:使用设计的AJAX提交表单第一次单击:使用设计的AJAX提交表单第三次单击:使用设计的AJAX提交表单第四次单击:刷新页面

etc. 等等

Any ideas on what I'm doing wrong here? 对我在这里做错的任何想法吗? I've made sure that the JS is present on all occasions, checked over everything, and googled my digits off - I just can't see where I've made an error? 我已确保所有情况下均使用JS,检查所有内容,并用手指搜索一下数字-我只是看不到哪里出错了?

Brilliant. 辉煌。

Many thanks to yuriy636 for providing the answer, and putvande for helping me to tidy up my secondary call. 非常感谢yuriy636提供了答案,并感谢putvande帮助我整理了第二通电话。

All works perfectly now, with 现在,所有功能都可以完美运行

$(document).on('submit', '.itempost', function(e) {

e.preventDefault();
$.ajax({
    type: "POST",
    url : 'item.php',
    data: $(this).serialize(),
    success : function (response) {
    $("#update").load("update.php?&t={$parentid}"); 

    },
    error: function (jXHR, textStatus, errorThrown) {
    alert('not ok');
        alert(errorThrown);
    }
});
return false;
});

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

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