修改ajax.beginform的onbegin发布输入值 [英] Modify posted input values on onbegin of ajax.beginform

查看:291
本文介绍了修改ajax.beginform的onbegin发布输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

能否贴在输入值onbegin ajax.beginform 的修改?
我提交表单后,修改了一些输入字段的值。但是,即使我的Request.Form 我得到它最初定在表格的递交时间的旧值通过JS改变价值观,在服务器端。如何获得的Request.Form修改后的值?

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. But even if I change the values through js, at server side in request.form I am getting the old values which were set initially at the time of form submit. How to get the modified values in request.form?

在code座如下:

<% using(Ajax.BeginForm("action", "controller",
     new AjaxOptions{onbegin="funBegin",oncomplete="funComplete"})){ 
%>

<input type="text" id="txtName" name="txtName" value="gaurav"/>
<input type="text" name="txtAge" value="26"/>
<input type="submit" value="click here" />

<% } %>

<script type="text/javascript">
    function funBegin() {
        $("#txtName").val("gaurav pandey");
    }
    function funBegin(result) {
        $("#divParent").html(result.get_data());
    }
</script>

现在,当我试图让的Request.Form [改为txtName] 在服务器端,我得到拉夫而不是拉夫潘迪。

Now when I try to get request.form["txtname"] at server side, I am getting "gaurav" instead of "gaurav pandey".

推荐答案

您遇到这个问题,因为 funBegin 表单数据已经系列化后调用。从<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.ajax.ajaxoptions.onbegin.aspxBlockquote\"相对=nofollow> MSDN :

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

AjaxOptions.OnBegin属性:获取或设置JavaScript函数的名称,页面更新之前,就立即打电话

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>

这篇关于修改ajax.beginform的onbegin发布输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆