2013-11-21 65 views
-2

我有5个选择框,并且我想要将最后4个选项选定值变为如下这样的变量:val+val+val+val 第一个选择框用于选择类别,以便其工作。带有多个选择框的jQuery过滤器搜索

下面你可以看到我的代码,但它不是完整的工作,因为如果我有没有选中我得到++++

<select name="Scat" id="Scat"> 
    <option value="">Select Manufacturer</option> 
    <option value="2222">Michelin</option> 
    <option value="3333">Continental</option> 
</select> 
<select name="Ssize" id="Ssize"> 
    <option value="">Select Size</option> 
    <option value="30">30</option> 
    <option value="40">40</option> 
</select> 
<select name="Sprofile" id="Sprofile"> 
    <option value="">Select Profile</option> 
    <option value="60">60</option> 
    <option value="80">80</option> 
</select> 
<a class="find" href="#">find</a> 
<script type="text/javascript"> 
$('.find').click(function(e){ 
    var Scat = $('#Scat option:selected').val(); 
    var allVariables = $("select option:selected").map(function(){ 
     return this.value 
    }).get().join("+"); 
    alert(allVariables); 
    if(Scat = 0){ 
     alert("Choose a Category"); 
    } else { 
     window.location = "http://something.com/category="+Scat+"/search="+allVariables; 
     //http://something.com/category=2222/search=60+30/ 
    }; 
}); 
</script> 

我缺少什么?

+0

http://jsfiddle.net/Kj6Rr/ –

+0

你不是[问这个早](http://stackoverflow.com/q/20120138/1612146)? – George

+0

是的,对不起,我只是想解决这个问题:( –

回答

1

第一点:您使用=作为比较运算符,但在JavaScript中必须使用=====

主要问题是由join('+')方法造成的。如果只有那些2名相关的下拉列表中,我建议手动输入他们每个人:

$('.find').click(function(e){ 
    var size, profile, Scat, searchString; 
    Scat = $('#Scat option:selected').val(); 

    if(!Scat){ 
     alert("Choose a Category"); 
     return; 
    } 

    size = $('#Ssize option:selected').val();  
    profile = $('#Sprofile option:selected').val(); 
    if size && profile) { 
     searchString = size + '+' + profile; 
    } else if (!size && !profile) { 
     searchString = ""; 
    } else if (!size) { 
     searchString = size; 
    } else if (!profile) { 
     searchString = profile; 
    } 

    window.location = "http://something.com/category="+Scat+"/search=" + searchString; 
}); 

Fiddel http://jsfiddle.net/Kj6Rr/5/

+0

谢谢,但'size'和'profile'不是必须的,那么如果没有选择这个选项,我怎样才能从'window.location'中删除这个变量? –

+0

检查编辑过的文章但是如果你在创建这个时遇到问题脚本,我建议你阅读一些教程。这是非常简单的脚本。 – Marvin

+0

这里是一个交互式教程:http://www.w3schools.com/js/default.asp – Marvin