Javascript下拉(选择)

时间:2011-11-09 21:23:17

标签: javascript html

所以,我有这段代码:

<form id="a">

<select name="day1" id="day1">
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
...
</select>

<select name="month1" id="month1">
<option value="01">Jan</option>
<option value="02">Feb</option>
<option value="03">Mar</option>
...
</select>

<select name="day2" id="day2">
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
...
</select>

<select name="month2" id="month2">
<option value="01">Jan</option>
<option value="02">Feb</option>
<option value="03">Mar</option>
...
</select>

<input type="submit" value="Submit" />

</form>

我需要脚本将浏览器重定向到

  

http://example.com/?from=AA-BB&to=XX-YY

表单提交后

AA将是选择区域中的选定值&#34; day1&#34;,

BB将是选择区域中的选定值&#34; month1&#34;,

XX将是选择区域中的选定值&#34; day2&#34;,

YY将是选择区域&#34; month2&#34;

中的选定值

提前致谢!

3 个答案:

答案 0 :(得分:1)

尝试以下

var day1 = $('#day1 option:selected').text();
var month1 = $('#month1 option:selected').text();
var day2 = $('#day2 option:selected').text();
var month2 = $('#month2 option:selected').text();
var suffix = 'from=' + day1 + '-' + month1 + '&to=' + day2 + '-' + month2;
window.location = 'http://mypage.com/?' + suffix;

答案 1 :(得分:0)

在普通的javascript中,您可以执行以下操作(在您表示的变量中分隔为变量):

function submitHandler() {
    var AA = document.a.day1.value;
    var BB = document.a.month1.value;
    var XX = document.a.day2.value;
    var YY = document.a.month2.value;

    location.href = "http://example.com/?from=" + AA + "-" + BB + "&to=" + XX + "-" + YY;
}

然后为您的表单设置onsubmit,如下所示:

<form name="a" onsubmit="submitHandler(); return false;">

答案 2 :(得分:0)

怎么样......

var sFrom="02-03",sTo="03-02";

var from = sFrom.split('-');
var to= sTo.split('-');
console.log(from);
console.log(to);

$('select#day1 > option[value="'+from[0]+'"]').attr('selected','selected');

$('select#month1 > option[value="'+from[1]+'"]').attr('selected','selected');


$('select#day2 > option[value="'+to[0]+'"]').attr('selected','selected');

$('select#month2 > option[value="'+to[1]+'"]').attr('selected','selected');

对于查询字符串检索,请检查this链接