简体   繁体   English

使用ajax加载部分视图,将触发提交按钮

[英]Loading partial view using ajax the submit button is fired

When I am loading a partial view with ajax the submit button is fired. 当我使用ajax加载部分视图时,将触发“提交”按钮。 If I remove/delete the submit button the partial view is rendered properly. 如果我删除/删除“提交”按钮,则部分视图将正确呈现。

This is my javascript code: 这是我的JavaScript代码:

$(".searchclientssendsms").keypress(function (event) {

    var keycode = (event.keycode ? event.keycode : event.which);
    if (keycode == '13') { //enter key

             var value = $(this).val();

             $.get("/smstemplate/SearchClients/" + value,

             function (data) {             
                $('#searchClientsResult').html(data);               
             });

    }
});

The line from RouteConfig regarding my search : 关于我的搜索,来自RouteConfig的一行:

routes.MapRoute(name: "SearchClientsSendSMS", 
                  url: "smstemplate/searchclients/{search}", 
             defaults: new { controller = "smstemplate",
                                 action = "searchclients" });

And the functions from controller: 以及来自控制器的功能:

    public ActionResult SendSMS()
    {
        return View();
    }


    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult SendSMS(SMSTemplate sms, string[] clients)
    {

        foreach (string client in clients)
        {
            int idclient = Convert.ToInt32(client);
            Customer customer=db.Customers.Find(idclient);
           // SendSMS(customer.Phone, sms.Text);
        }

        return View();
    }


    public ActionResult SearchClients(string search)
    {
        List<Customer> clients = db.Customers.Where(c => c.Deleted == true && (c.Email.Contains(search) || c.Name.Contains(search) || c.Phone.Contains(search))).ToList();

        return PartialView(clients);
    }

If I execute the javascript function from firebug the partial view is rendered corrected. 如果我从萤火虫执行javascript函数,则部分视图将被更正。 The submit button is not fired any more. 提交按钮不再触发。

 $.get("/smstemplate/SearchClients/poir",

        function (data) {

            $('#searchClientsResult').html(data);

        });

I noticed this behaviour only at enter key. 我仅在输入键时才注意到此行为。

So, what can cause that? 那么,是什么原因引起的呢?

It seems to me that the Enter key is also submitting the button, so it is calling the AJAX and pressing button at the same time. 在我看来, Enter键也正在提交按钮,因此它正在同时调用AJAX并按下按钮。

Try this: 尝试这个:

$(".searchclientssendsms").keypress(function (event) {

var keycode = (event.keycode ? event.keycode : event.which);
    if (keycode == '13') { //enter key

         var value = $(this).val();

         $.get("/smstemplate/SearchClients/" + value,

         function (data) {             
            $('#searchClientsResult').html(data);               
         });

         event.preventDefault(); //Inside the if so only is prevented for Enter Key.

    }
});

And to disable it in all the page you could try this: 要在所有页面中将其禁用,您可以尝试以下操作:

$(document).ready(function() {
  $(window).keydown(function(event){
    if(event.keyCode == 13) {
      event.preventDefault();
      return false;
    }
   });
});

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

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