多选一个optgroup并显示相应的计数字段?

时间:2014-09-25 10:24:22

标签: jquery multi-select optgroup

我使用jquery + bootstrap + bootstrapselect来开发表单。但现在我被卡住了。我想显示一个计数字段foreach选择。例如,如果选择了mustard,relish和steamed,我想显示3个具有相应名称的计数输入字段,并将组合存储在mysql中。

我正在尝试jquery-interdependencies但是没有让它工作。

这是optgroup

 <select id="food" name="food[]" class="selectpicker" multiple>
    <optgroup label="Condiments">
      <option>Mustard</option>
      <option>Ketchup</option>
      <option>Relish</option>
    </optgroup>
    <optgroup label="Breads">
      <option>Plain</option>
      <option>Steamed</option>
      <option>Toasted</option>
    </optgroup>
  </select>

和jquery -interdepencies部分(仅适用于一个选择)

$(document).ready(function() {
            // Start creating a new ruleset
            var ruleset = $.deps.createRuleset();

            // Show 
            var foodRuleMustard = ruleset.createRule("#food", "==", "mustard");
            foodRuleMustard.include("#num_of_mustard");
            var foodRuleKetchup = ruleset.createRule("#food", "==", "ketchup");
            foodRuleKetchup.include("#num_of_ketchup");
            var foodRuleRelish = ruleset.createRule("#food", "==", "relish");
            foodRuleRelish.include("#num_of_relish");
            //etc

            // Make the ruleset effective on the whole page
            ruleset.install({log: true});
        });

尝试制作一个jsfiddle,但这也不起作用:http://jsfiddle.net/9p062q9m/

1 个答案:

答案 0 :(得分:0)

好的,我放弃了jquery -interdepencies,只是在num_of输入字段中添加了一个class =“colors” 在每个循环之前隐藏class =“colors”输入字段

`       $(document).ready(function() {

                  $("#food").change(function () {
                       $('.foods').hide();
                      $("#food option:selected").each(function(){
                          var str = $(this).attr("value");
                            $('#num_of_'+str).show();
                    });
                  });

            });`