开发者

Adding dynamic parameters with Html.BeginForm and jQuery submit

开发者 https://www.devze.com 2023-01-17 12:29 出处:网络
// html <% using (Html.BeginForm(\"MyAction\", \"MyController\", new { id = ViewContext.RouteData.Values[\"id\"] },
// 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

精彩评论

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