JavaScript:如何根据选择更改表单操作属性值?

时间:2022-09-20 13:14:44

I'm trying to change the form action based on the selected value from a dropdown menu.

我正在尝试根据下拉菜单中的选定值更改表单操作。

Basically, the HTML looks like this:

基本上,HTML看起来像这样:

<form class="search-form" id="search-form" method="post" accept-charset="UTF-8" action="/search/user">
<select id="selectsearch" class="form-select" name="selectsearch">
<option value="people">Search people</option>
<option value="node">Search content</option>
</select>

<label>Enter your keywords: </label>
 <input type="text" class="form-text" value="" size="40" id="edit-keys" name="keys" maxlength="255" />

<input type="submit" class="form-submit" value="Search" id="edit-submit" name="search"/>
</form>

If "people" is selected, (which it is by default), the action should be "/search/user", and if content is selected, the action should be "/search/content"

如果选择“人员”(默认情况下),则操作应为“/ search / user”,如果选择了内容,则操作应为“/ search / content”

I'm still searching around, but haven't been able to find out how to do this.

我还在四处寻找,但一直无法找到如何做到这一点。

4 个解决方案

#1


271  

$("#selectsearch").change(function() {
  var action = $(this).val() == "people" ? "user" : "content";
  $("#search-form").attr("action", "/search/" + action);
});

#2


23  

If you only want to change form's action, I prefer changing action on-form-submit, rather than on-input-change. It fires only once.

如果您只想更改表单的操作,我更喜欢更改操作on-form-submit,而不是on-input-change。它只发射一次。

$('#search-form').submit(function(){
  var formAction = $("#selectsearch").val() == "people" ? "user" : "content";
  $("#search-form").attr("action", "/search/" + formAction);
}); 

#3


0  

It's better to use

最好使用

$('#search-form').setAttribute('action', '/controllerName/actionName');

rather than

$('#search-form').attr('action', '/controllerName/actionName');

So, based on trante's answer we have:

所以,根据trante的回答,我们有:

$('#search-form').submit(function() {
    var formAction = $("#selectsearch").val() == "people" ? "user" : "content";
    $("#search-form").setAttribute("action", "/search/" + formAction);
}); 

Using setAttribute can save you a lot of time potentially.

使用setAttribute可以为您节省大量时间。

#4


-15  

Is required that you have a form?
If not, then you could use this:

要求你有表格吗?如果没有,那么你可以使用这个:

<div>
    <input type="hidden" value="ServletParameter" />
    <input type="button" id="callJavaScriptServlet" onclick="callJavaScriptServlet()" />
</div>

with the following JavaScript:

使用以下JavaScript:

function callJavaScriptServlet() {
    this.form.action = "MyServlet";
    this.form.submit();
}

#1


271  

$("#selectsearch").change(function() {
  var action = $(this).val() == "people" ? "user" : "content";
  $("#search-form").attr("action", "/search/" + action);
});

#2


23  

If you only want to change form's action, I prefer changing action on-form-submit, rather than on-input-change. It fires only once.

如果您只想更改表单的操作,我更喜欢更改操作on-form-submit,而不是on-input-change。它只发射一次。

$('#search-form').submit(function(){
  var formAction = $("#selectsearch").val() == "people" ? "user" : "content";
  $("#search-form").attr("action", "/search/" + formAction);
}); 

#3


0  

It's better to use

最好使用

$('#search-form').setAttribute('action', '/controllerName/actionName');

rather than

$('#search-form').attr('action', '/controllerName/actionName');

So, based on trante's answer we have:

所以,根据trante的回答,我们有:

$('#search-form').submit(function() {
    var formAction = $("#selectsearch").val() == "people" ? "user" : "content";
    $("#search-form").setAttribute("action", "/search/" + formAction);
}); 

Using setAttribute can save you a lot of time potentially.

使用setAttribute可以为您节省大量时间。

#4


-15  

Is required that you have a form?
If not, then you could use this:

要求你有表格吗?如果没有,那么你可以使用这个:

<div>
    <input type="hidden" value="ServletParameter" />
    <input type="button" id="callJavaScriptServlet" onclick="callJavaScriptServlet()" />
</div>

with the following JavaScript:

使用以下JavaScript:

function callJavaScriptServlet() {
    this.form.action = "MyServlet";
    this.form.submit();
}