Javascript jQuery未拾取下拉列表的选定选项值
我有一个简单的两个下拉表单,需要根据下拉列表中的给定值重定向Javascript jQuery未拾取下拉列表的选定选项值,javascript,jquery,html,Javascript,Jquery,Html,我有一个简单的两个下拉表单,需要根据下拉列表中的给定值重定向 jQuery(文档).ready(函数(){ var location=jQuery('#位置选项:selected').val(); var style=jQuery('#style选项:selected').val(); jQuery(“#DropSearch”)。单击(函数(e){ e、 预防默认值(); e、 停止传播(); window.location.href=http://showroomworldwide.com/
jQuery(文档).ready(函数(){
var location=jQuery('#位置选项:selected').val();
var style=jQuery('#style选项:selected').val();
jQuery(“#DropSearch”)。单击(函数(e){
e、 预防默认值();
e、 停止传播();
window.location.href=http://showroomworldwide.com/?geodir_search=1&stype=gd_place&s=“+location+”&stype=gd_-place&s=”+style;
});
});代码>
东北部
西部
中西部
南方
工业的
农舍
当代的
海滩风格
传统的
您不需要测试所选选项。只需获取select的值。val()
但是,您必须在单击按钮时获取这些值,而不是在单击之前。
另外,由于您没有使用提交按钮,因此没有需要取消的本机行为,因此您可以删除e.preventDefault()
和e.stopPropagation()
$(函数(){
$(“#DropSearch”)。单击(函数(e){
//现在按钮已经点击,只需获取
//元素的值。无需获取所选选项
var location=jQuery('#location').val();
var style=jQuery('#style').val();
//测试以查看是否存储了正确的选项:
控制台日志(位置、样式);
window.location.href=
'http://showroomworldwide.com/?geodir_search=1&stype=gd_place&s=' +
位置+'&stype=gd_位置&s='+style;
});
});代码>
东北部
西部
中西部
南方
工业的
农舍
当代的
海滩风格
传统的
您在文档准备就绪的开始处立即执行这些行。因此,它们的值将是页面加载时的值。它们不会根据单击逻辑进行重新评估。如果要按单击时的状态拾取值,请将这些值移动到单击逻辑中
var location = jQuery('#Location option:selected').val();
var style = jQuery('#Style option:selected').val();