Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 验证后发送表单_Jquery_Asp.net Mvc - Fatal编程技术网

Jquery 验证后发送表单

Jquery 验证后发送表单,jquery,asp.net-mvc,Jquery,Asp.net Mvc,我是MVC的新手,所以我很难解释,很抱歉 我有一个表单,我喜欢在使用post方法发送之前进行验证。该表单位于Index.cshtml文件中,并发布到HomeController。 我的问题是,当我发送表单时,它没有到达正确的index()方法(HomeController中带有CallMeNow请求的第二个方法) 有没有可能用这种方法解决这个问题 这是HomeController.cs文件中的内容: // GET: /Home/ [HttpGet] public View

我是MVC的新手,所以我很难解释,很抱歉

我有一个表单,我喜欢在使用post方法发送之前进行验证。该表单位于Index.cshtml文件中,并发布到HomeController。 我的问题是,当我发送表单时,它没有到达正确的index()方法(HomeController中带有CallMeNow请求的第二个方法)

有没有可能用这种方法解决这个问题

这是HomeController.cs文件中的内容:

    // GET: /Home/
    [HttpGet]
    public ViewResult Index()
    {
        return View();
    }



    // Send CallMeNow Request.
    [HttpPost]
    public ViewResult Index(string txtCallMeNow)
    {
        if (txtCallMeNow != null)
        {
            // .. doing something here.
        }
        ViewBag.EmailSent = "Thank you.";

        return View();
这是我在Index.cshtml文件中看到的:

  • 这将验证表单并将其发送:

    $('#callMeNow1').submit(function (e) {
        e.preventDefault();  
        var selectedCity = $('#txtCallMeNow').val();
        if (selectedCity == 'enter phone number...')
            alert('enter phone number!');                               
        else
            document.location = $(this).prop('action');
    });
    
  • 这是表格。应将其张贴在HomeController上:

    @using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "callMeNow1" }))
        {
            <input type="text" id="txtCallMeNow" name="txtCallMeNow" onfocus="if(this.value != '') {this.value = '';}"
                onblur="if (this.value == '') {this.value = 'enter phone number...';}" value=" enter phone number..." /><br />
            <input type="submit" class="call_me_now" value="" /> 
    
             @ViewBag.EmailSent;
        }
    
    @使用(Html.BeginForm(“Index”,“Home”,FormMethod.Post,new{id=“callMeNow1”}))
    {
    
    @ViewBag.EmailSent; }

  • 谢谢

    您应该移动
    e.preventDefault()内部if条件。因为它将停止表单提交。我相信您希望在验证失败的情况下停止表单提交

    $('#callMeNow1').submit(function (e) {
    
        var selectedCity = $('#txtCallMeNow').val();
        if (selectedCity == 'enter phone number...'){
            e.preventDefault(); 
            alert('enter phone number!'); 
        }                             
    
    });