简体   繁体   English

Ajax表单提交刷新页面

[英]ajax form submission refreshes page

I'm working on this project and am attempting to make a call to a controller function without performing a page refresh. 我正在从事这个项目,并且尝试在不执行页面刷新的情况下调用控制器功能。 I tried setting up an ajax function in order to do this but haven't had any luck. 我尝试设置ajax函数来执行此操作,但是没有任何运气。 I've placed the related script both in the html and in a separate jquery.js but to no avail. 我已经将相关的脚本放置在html和单独的jquery.js中,但无济于事。 It appears as if the form submission just isn't hitting the script at all. 似乎表单提交根本没有击中脚本。 Any help would be greatly appreciated!! 任何帮助将不胜感激!! The form is in a cshtml file, controller in .cs, and script in .js 表单位于cshtml文件中,控制器位于.cs中,脚本位于.js中

the form in question: 有问题的表格:

<form id="tableForm"  action="@(Url.Action("AddToOrder", "Order"))" method="post">
                                    @{foreach (var item in (IList<Item>)ViewData["items"]){
                                    <input type="hidden" id="@("item_id_" + item.id)" name="@("item_id_" + item.id)" value="@item.id" />
                                    <tr>
                                        <td>@item.description</td>
                                        <td>@String.Format("{0:C}",item.price)</td>
                                        <td><input type="text" id="@("item_quantity_" + @item.id)" name="@("item_quantity_" + @item.id)" style="width: 50px;" value="0" /></td>
                                     </tr>
                                      }}
                                    <tr>
                                        <td><input class="submit" type="submit" value="Add item(s) to order" /></td>
                                    </tr>
                                </form>

the script in question: 有问题的脚本:

$('#tableForm').submit(function (e) {
e.preventDefault();
    $.ajax({
        type: "POST",
        cache: false,
        url: $(this).attr('action'),
        data: $(this).serialize(),
        succes: function (data) {
            alert(data);
        },
        error: function (jXHR, textStatus, errorThrown) {
            alert(errorThrown);
        }
    });
});

To reiterate, the controller and method are working great, but the form submission refreshes the page. 重申一下,控制器和方法都很好用,但是表单提交刷新了页面。

refer the below code, call the preventdetfault after the submission 参考下面的代码,提交后调用preventdetfault

$('#tableForm').submit(function (e) {
    $.ajax({
        type: "POST",
        cache: false,
        url: $(this).attr('action'),
        data: $(this).serialize(),
        succes: function (data) {
            alert(data);
        },
        error: function (jXHR, textStatus, errorThrown) {
            alert(errorThrown);
        }
    });
    e.preventDefault();
    });

Is your AJAX method getting called on page submission. 您的AJAX方法是否在页面提交时被调用? Try adding an alert to confirm that. 尝试添加警报以确认这一点。 You might want to add ClientIdMOde="static" to your form tag, so that if it is with in master page the control id will be static and JS can bind to it. 您可能希望将ClientIdMOde =“ static”添加到您的表单标签中,这样,如果它在母版页中带有,则控件ID将是静态的,并且JS可以绑定到它。

May be clientidmode will do the trick. 可能由clientidmode可以解决。 Let me know if it doesn't 让我知道是否可以

Without your full page shown, I can only make an educated guess: that you have included your .js file at the top of the page or in the head element . 没有显示完整页面,我只能做出有根据的猜测:您已经在页面顶部或head元素中包含了.js文件。

That means that the following code does nothing (returns an empty jQuery object): 这意味着以下代码不执行任何操作(返回一个空的jQuery对象):

$('#tableForm')

That is because the code runs as soon as it is loaded/defined and the form element it references has not been loaded yet. 这是因为代码在加载/定义后立即运行,并且其引用的form元素尚未加载。

The simply solution is: to either put your JS code at the end of the body element, or wrap it in a DOM-ready event handler. 简单的解决方案是:将您的JS代码放在body元素的末尾,或将其包装在支持DOM的事件处理程序中。 The handler is preferred as you can be moved about the page without failing: 首选处理程序,因为您可以在页面上移动而不会失败:

eg 例如

$(function(){
   // your jQuery code here can now use the DOM safely
});

which is a shortcut for this: 这是一个捷径:

$(document).ready(function(){
   // your jQuery code here can now use the DOM safely
});

That means you code will now look like: 这意味着您的代码现在将如下所示:

$(function(){
    $('#tableForm').submit(function (e) {
        e.preventDefault();
        $.ajax({
            type: "POST",
            cache: false,
            url: $(this).attr('action'),
            data: $(this).serialize(),
            succes: function (data) {
                alert(data);
            },
            error: function (jXHR, textStatus, errorThrown) {
                alert(errorThrown);
            }
        });
    });
});

If it still fails, the usual cause in a Javascript error preceding your code (missing jQuery library include etc). 如果仍然失败,则通常是代码之前的Javascript错误(缺少jQuery库include等)引起的。 You will need to show your entire page to clarify that though. 不过,您需要显示整个页面以进行澄清。

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

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