Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery JavaScript:如何根据选择更改表单动作属性值?_Jquery_Forms_Dynamic_Action - Fatal编程技术网

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

Jquery JavaScript:如何根据选择更改表单动作属性值?,jquery,forms,dynamic,action,Jquery,Forms,Dynamic,Action,我正在尝试根据下拉菜单中的选定值更改窗体操作 基本上,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">Se

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

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

搜身
搜索内容
输入您的关键字:
如果选择了“人员”(默认情况下),则操作应为“/search/user”,如果选择了内容,则操作应为“/search/content”


我还在四处搜索,但还没有找到如何做到这一点

您需要一张表格吗?
$("#selectsearch").change(function() {
  var action = $(this).val() == "people" ? "user" : "content";
  $("#search-form").attr("action", "/search/" + action);
});
如果没有,那么您可以使用:


使用以下JavaScript:

函数callJavaScriptServlet(){
this.form.action=“MyServlet”;
这个.form.submit();
}

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

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

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

$('#search-form').attr('action', '/controllerName/actionName');
因此,根据特兰特的回答,我们有:

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

使用
setAttribute
可以潜在地节省大量时间。

在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>

document.getElementById(“selectsearch”).addEventListener(“更改”,函数(){
var get_form=document.getElementById(“搜索表单”)//获取表单
get_form.action='/search/'+this.value;//赋值
});

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

函数fn_one(){
document.formname.action=”https://google.com/search?q=form+提交+使用+名称”;
}
函数fn_two(){
document.getElementsByName(“formname”)[0]。操作=“https://google.com/search?q=form+提交+使用+名称+方法+2”;
}
函数fn_三(){
document.getElementById(“formid”).action=”https://google.com/search?q=form+提交+使用+ID”;
}

按名称更改{1}
按名称更改{2}
按ID更改
去谷歌

这比我尝试的要好得多。。。谢谢你工作得很好。谢谢我更新了此代码以更改提交时的操作,而不是更改表单。如果您是为移动设备设计的,则最好将操作更改为“提交时”以解决性能问题。检查下面我的答案。使用
prop
而不是
attr
。示例=
$(“#搜索表单”).prop(“操作”,“搜索/”+操作)
this.form.submit();没有意义,因为实际上没有要提交的表单,因此没有任何东西使用jquery
attr
可以节省时间吗?