开发者

Adding dynamic parameters with Html.BeginForm and jQuery submit

// html
<% using (Html.BeginForm("MyAction", "MyController", 
                   new { id = ViewContext.RouteData.Values["id"] },
                   FormMethod.Post, 
                   new { enctype = "multipart/form-data", class="myForm" }))
 { %>
    <input type="file" name="blah" />
 <% } %>



// script
$container.find('.myButton').click(function() {
    $container.find('.myForm').submit();
});

Before the form is submitted, I need to add some extra parameters (ro开发者_如何学Pythonute values) which can only be calculated at the time of submit.

How do I do that?


You could append a hidden field to the form before submitting it:

$container.find('.myButton').click(function() {
    var form = $container.find('.myForm');
    form.append(
        $(document.createElement('input'))
            .attr('type', 'hidden')
            .attr('name', 'somename')
            .attr('type', 'somecalculatedvalue')
    );
    form.submit();
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜