jQuery JavaScript:如何根据选择更改表单操作属性值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1925614/
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
JavaScript: how to change form action attribute value based on selection?
提问by n00b0101
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"
如果选择了“people”(默认情况下),则操作应为“/search/user”,如果选择内容,则操作应为“/search/content”
I'm still searching around, but haven't been able to find out how to do this.
我仍在四处寻找,但一直无法找到如何做到这一点。
回答by cletus
$("#selectsearch").change(function() {
var action = $(this).val() == "people" ? "user" : "content";
$("#search-form").attr("action", "/search/" + action);
});
回答by trante
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.
如果您只想更改表单的操作,我更喜欢在表单提交时更改操作,而不是在输入更改时。它只触发一次。
$('#search-form').submit(function(){
var formAction = $("#selectsearch").val() == "people" ? "user" : "content";
$("#search-form").attr("action", "/search/" + formAction);
});
回答by scode102
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
可能会为您节省大量时间。
回答by Ankush Kumar
Simple and easy in javascipt
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>
回答by Dago Jobs
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();
}