151

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

基本上,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>

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

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

4

6 回答 6

277
$("#selectsearch").change(function() {
  var action = $(this).val() == "people" ? "user" : "content";
  $("#search-form").attr("action", "/search/" + action);
});
于 2009-12-18T00:54:04.490 回答
25

如果您只想更改表单的操作,我更喜欢在表单提交时更改操作,而不是在输入更改时更改操作。它只触发一次。

$('#search-form').submit(function(){
  var formAction = $("#selectsearch").val() == "people" ? "user" : "content";
  $("#search-form").attr("action", "/search/" + formAction);
}); 
于 2013-05-25T13:44:42.847 回答
1

javascipt中的简单易行

<script>

  document.getElementById("selectsearch").addEventListener("change", function(){

  var get_form =   document.getElementById("search-form") // get form 
  get_form.action =  '/search/' +  this.value; // assign value 

});

</script>
于 2020-02-04T10:15:28.767 回答
0

最好用

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

而不是

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

因此,根据 trante 的回答,我们有:

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

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

于 2018-03-06T19:13:30.480 回答
0

如果您想从纯 javascript 进行更改,请使用以下方法。与带有额外方法的 Ankush Kumar 的代码相同

function fn_one(){
  document.formname.action="https://google.com/search?q=form+submit+using+name";
}
function fn_two(){
  document.getElementsByName("formname")[0].action="https://google.com/search?q=form+submit+using+name+method+2";
}
function fn_three(){
  document.getElementById("formid").action="https://google.com/search?q=form+submit+using+ID";
}
<form name="formname" id="formid" action="google.com/search?q=" target="_blank">
  <div>
    <button type="button" onclick="fn_one()">Change By Name {1}</button>
    <button type="button" onclick="fn_two()">Change By Name {2}</button>
    <button type="button" onclick="fn_three()">Change By ID</button>
  </div>
   <div>
    <button type="submit">Go To Google</button>
  </div>
</form>
    

于 2021-04-27T10:58:49.083 回答
-16

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

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

使用以下 JavaScript:

function callJavaScriptServlet() {
    this.form.action = "MyServlet";
    this.form.submit();
}
于 2011-12-07T18:26:31.327 回答