我正在尝试根据从下拉菜单中选择的值更改表单操作。
基本上,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”
我还在四处寻找,但一直没能找到如何做到这一点。
最佳答案
$("#selectsearch").change(function() {
var action = $(this).val() == "people" ? "user" : "content";
$("#search-form").attr("action", "/search/" + action);
});
关于jquery - JavaScript : how to change form action attribute value based on selection?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1925614/