Filter three select boxes based on previous selections

天大地大妈咪最大 提交于 2019-12-25 18:37:41

问题


I am trying to change select boxes in order based on what is previously selected. I have it working for the first one to change but not the third. If someone can help me make third one work that would be great.

http://jsfiddle.net/Ze5AA/21/

Here is the js I am using:

$("#CAT_Custom_496270").change(function() { 
    if($(this).data('options') == undefined){
        $(this).data('options',$('#CAT_Custom_495029 option').clone());
    } 

    var id = $(this).val();
    var options = $(this).data('options').filter('[value*=' + id + ']');
    $('#CAT_Custom_495029').html(options);
});

$("#CAT_Custom_495029").change(function() { 
    if($(this).data('options') == undefined){
        $(this).data('options',$('#CAT_Custom_495038 option').clone());
    } 
    var id = $(this).val();
    var options = $(this).data('options').filter('[value*=' + id + ']');
    $('#CAT_Custom_495038').html(options);
});

回答1:


Wrap your value attribute filter's value in quotes, since there is a space in the values.

var options = $(this).data('options').filter('[value*="' + id + '"]');

Fiddle




回答2:


your values aren't consistant for what you are checking for because of the spaces.

you can wrap it in quotes, though I would only just get the value you need (the first 3 characters) to keep it cleaner

you need to split the check, I would add a delimiter to make it easy.

 //html example from menu 2
    <option value="CSD: 0-45">0-45</option>
        <option value="CSD: 46-50">46-50</option> //note the colon after CSD

 //html from menu 3

<option value="CSD: $25.40/month /20 Year Pay">$25.40/month /20 Year Pay</option>

<option value="CSD: $26.05/month /20 Year Pay">$26.05/month /20 Year Pay</option>

<option value="CSD: 51 $27.50/month /20 Year Pay">$27.50/month /20 Year Pay</option> //note the color again


 //the java

   var id = $(this).val().split(":"); //makes a 2 value array split at the colon
   var options = $(this).data('options').filter('[value*=' + id[0] + ']'); //we want the first array value (before the colon)


来源:https://stackoverflow.com/questions/17602535/filter-three-select-boxes-based-on-previous-selections

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!