JavaScript:如何根据选择更改表单操作属性值? [英] JavaScript: how to change form action attribute value based on selection?

查看:26
本文介绍了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);
});

这篇关于JavaScript:如何根据选择更改表单操作属性值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆