Modify posted input values on onbegin of ajax.beginform

前端 未结 4 1077
庸人自扰
庸人自扰 2020-12-17 16:49

Can posted input values on onbegin of ajax.beginform be modified? I have to modify values of some of the input fields after the form is submitted.

相关标签:
4条回答
  • 2020-12-17 17:02

    There is another way to do that :

    function funBegin(jqXHR, settings) {
        // before POST - change whatever posted value you want and then 
      // serialize again your form supposedly identified here by form
    
            settings.data = form.serialize();
    
      // the advantage is when you arrive here, the form validators have already been executed
      // and you know you have valid values - so you can post
        }
    }
    

    For meaning of parameters, see [http://api.jquery.com/jquery.ajax/]

    0 讨论(0)
  • 2020-12-17 17:15

    You're having this issue because funBegin is called after the form data has been serialized. From MSDN:

    AjaxOptions.OnBegin Property: Gets or sets the name of the JavaScript function to call immediately before the page is updated.

    I suggest you write your own submit handler:

    <form id="myform" action="/Home/MyAction">
        <input type="text" id="txtName" name="txtName" value="gaurav" />
        <input type="submit" value="Submit" />
    </form>
    
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $('#myform').submit(function() {
                $("#txtName").val("gaurav pandey");
    
                var form = $(this);
                var url = form.attr('action');
                var formData = form.serialize();
                $.post(url, formData, function(result) {
                    // Do something with result
                });
    
                return false;
            });
        });
    </script>
    
    0 讨论(0)
  • 2020-12-17 17:15

    I didn't feel like going outside of MVC's form submission handling so I solved this differently. Just bind to the form's submit button's click handler. Do your input value fiddling there. The onclick is fired before MVC's form serialization. The onclick also gets called even if the form is submitted by the user hitting enter.

    0 讨论(0)
  • 2020-12-17 17:21

    $('#myform').submit() won't work on IE. If there is any validation, you can try to create a fake validation method to collecting form data.

    like Jquery.validaton below:

    jQuery.validator.addMethod("fakeMethod", function (value, element) {
                    YOUR DOM CODE HERE!!!
                    return true;
                }, "");
    
    0 讨论(0)
提交回复
热议问题