javascript 如何在表单提交中更改 Html.Beginform() 的属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18487154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 12:07:38  来源:igfitidea点击:

How to change attribute of Html.Beginform() on form Submitting

javascriptjqueryasp.net-mvc

提问by Daniel Donev

I have this so far:

到目前为止我有这个:

 <% foreach (Object object in Collection)
 {
     u<% using (Html.BeginForm("ActionName", "Controller", new { FU = "bar" }, FormMethod.Post, new { ID = "MyID"}))
      {%>
        <input type="submit" value="Submit" />
      <%}
 } %>

 $('#MyID').submit(function() {
     var url = Url.Action("ActionName", "ControllerName");
     var fu = "newValueOfFU"; // New value for FU
     $('#MyID').prop('action', url + "?FU=" + fu);
 });

I want to change the value of FU with the value from jQuery

我想用 jQuery 的值更改 FU 的值

回答by Kami

Simplified Answer.

简化答案。

You are using the incorrect overload. See the overloads list on MSDNfor an idea of which to use.

您正在使用不正确的过载。请参阅MSDN上的重载列表以了解要使用的重载列表。

Your current overloads expects routing information as the 3rd parameter. Any values that you provide will be matched against the routes defined for the site. If any parameters do not match it, they will be simply added as get parameters.

您当前的重载需要路由信息作为第三个参数。您提供的任何值都将与为站点定义的路由匹配。如果任何参数不匹配,它们将被简单地添加为获取参数。

What you are trying to achieve is to assign an IDor another attribute to the form. This is done using an overload which allows for html attributes to be defined (see Overload on MSDN. eg,

您试图实现的是为ID表单分配一个或另一个属性。这是使用重载完成的,该重载允许定义 html 属性(请参阅MSDN 上的重载。例如,

@using(Html.BeginForm("Action", "Controller", FormMethod.POST, new {ID = "MyID"}))

Note: The order of parameters is Different.

注意:参数顺序不同

Simply look at the html generated and you will get an idea of what is happening.

只需查看生成的 html,您就会了解正在发生的事情。

// Update

// 更新

Use this

用这个

@using(Html.BeginForm("Action", "Controller", new {FU="bar"}, FormMethod.POST, new {ID = "MyID", onsubmit="return sayHello()"}))

This will generate html similar to

这将生成类似于

 <form action="/Controller/Action?FU=Hello" method="post" id="MyID" onsumbit="return sayHello()">

and combined with script

并结合脚本

<script>
    function sayHello()
    {
        alert("Hello");
        return true;
    }
</script>

will give you alert, as well as sending FUto controller.

会给你警报,以及发送FU给控制器。

// Update This is how you can update the action attribute of a form.

// 更新 这是更新表单的操作属性的方法。

<script>
  $('#MyID').submit(function() {
      var url = @Url.Action("Action","Controller");
      var fu = "newValueOfFU"; // New value for FU
      $('#MyID').prop('action', url + "?FU=" + fu;
      return true;
  });
</script>

回答by U.P

You can use the following jQuery code

您可以使用以下 jQuery 代码

$('form').submit(function(){
   $(this).attr('ID', newVal);
});

Or you can assign an ID to your form like

或者您可以为您的表单分配一个 ID,例如

using (Html.BeginForm("ActionName", "Controller", FormMethod.Post, new { @id = "myForm", FU = "Hello" }))

and use it to hook an event on submit

并使用它在提交时挂钩一个事件

$('#myForm').submit(function(){
       $(this).attr('ID', newVal);
    });

回答by Xmindz

You can add an onSubmit event handler in the HTMLAttributes and change the ID in that.

您可以在 HTMLAttributes 中添加 onSubmit 事件处理程序并更改其中的 ID。

<% using (Html.BeginForm("ActionName", "Controller", new { ID = "Hello", onsubmit = "return changeID();"  }, FormMethod.Post))
           {%>
        <input type="submit" value="Submit" />
        <%} %>

And add the following script somewhere in your page.

并在页面中的某处添加以下脚本。

<script type="text/javascript">
    function changeID() {
           document.getElementById('Hello').id  = 'Your_New_Value';         

           return true;
    }
</script>

回答by Iván Ramos Jiménez

<script type="text/javascript">
$(document).ready(function () {

    $('#formID').submit(function (e) {
        e.preventDefault();
         $('#yourOtherID').attr(newValue);
    });

});
</script>