JavaScript:如何根据选择更改表单动作属性值?
问题描述:
我正在尝试根据下拉菜单中选择的值来更改表单操作.
I'm trying to change the form action based on the selected value from a dropdown menu.
基本上,HTML看起来像这样:
Basically, the HTML looks like this:
<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"
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"
我仍在搜索,但无法找到如何执行此操作.
I'm still searching around, but haven't been able to find out how to do this.
答
$("#selectsearch").change(function() {
var action = $(this).val() == "people" ? "user" : "content";
$("#search-form").attr("action", "/search/" + action);
});