jQuery根据选定的值更改表单操作

时间:2013-03-27 16:03:18

标签: php javascript jquery

在使用方法在值更改时设置表单操作时遇到了一个非常棘手的问题。我有以下代码:

var actions = new Array();
actions[0] = "some-site.html";
actions[1] = "some-other-site.html";
actions[2] = "another-site.html";
etc....

$("select#feld-urlaubsart").change(function () {
if ($("select#fieldname").val() !== "") {
$("form#searchform").attr("action","http://www.mysite.com/" +  
actions[$("select#fieldname").val()]);
}
});

另一方面,我的选择如下:

<select id="fieldname" name="fieldname">
<option value="0" selected="selected">All</option>
<optgroup label="Golfurlaub mit Greenfees">
<option value="1">Alle Greenfee-Angebote</option>
<option value="2">Top-Angebote</option>
<option value="3">Golfurlaub mit 4 Tage Greenfee</option>
</optgroup>

实际上它确实有效,但我对它不满意,因为我需要依赖于value =“1”value =“2”等。

现在我需要使用实际描述作为值来与我传递值的另一个脚本兼容,以便在PHP / JSON中使用。我需要像这样的数据

<option value="All">All</option>
<option value="Option 1">Option 1</option>
etc

如何根据所选的VALUE更改表单的操作,而不需要使用“1”“2”等数组键?

感谢您的任何建议。

1 个答案:

答案 0 :(得分:0)

使用json对象:

actions = {"All":"some-site.html","Option1":"some-other-site.html"}

然后

actions["All"]

"some-site.html"
相关问题