问题描述:

I need to set the action a form posts to using javascript. I tried to do this:

 $('form').get(0).setAttribute('action', formAction);

and

 $("#form").attr('action', formAction);

In both cases, the query is malformed, like this:localhost:xxxx//controller/action1/action2

Why does this happen? How can I correctly set the action the form posts to ?

The complete javascript function is this

function GetNotifType() {

formAction = "EditWithDifferentType";

$("#form").attr('action', formAction);

$("#form").submit();

}

网友答案:

I used @Url.Action() to get the fully qualified Url for the action, and it worked:

formAction = '@Url.Action("EditWithDifferentType", "Notifications")';
$("#form").attr('action', formAction);
$("#form").submit();
相关阅读:
Top