当select为hide时,该值始终具有默认值

时间:2014-06-10 06:26:44

标签: jquery select checkbox

<input type="checkbox" name="seeds-a1" value="Indigofera" /> <span>Indigofera</span>

//this select will show if checkbox mark as checked 

<select class="selectDrop" name="orders">
  <option value="">Select</option>
  <option>1/2 kg</option>
  <option>1 kg</option>
  <option>2 kg</option>
  <option>3 kg</option>
  <option>4 kg</option>
  <option>5 kg</option>
</select>

<input type="checkbox" name="seeds-a2" value="calliandra" /> <span>calliandra</span>

    //this select will show if checkbox mark as checked 

    <select class="selectDrop" name="orders2">
      <option value="">Select</option>
      <option>1/2 kg</option>
      <option>1 kg</option>
      <option>2 kg</option>
      <option>3 kg</option>
      <option>4 kg</option>
      <option>5 kg</option>
    </select>

   <input type="checkbox" name="seeds-a3" value="ipil-ipil" /> <span>ipil-ipil</span>

    //this select will show if checkbox mark as checked 

    <select class="selectDrop" name="orders3">
      <option value="">Select</option>
      <option>1/2 kg</option>
      <option>1 kg</option>
      <option>2 kg</option>
      <option>3 kg</option>
      <option>4 kg</option>
      <option>5 kg</option>
    </select>

使用此 js 选择将隐藏并显示我的问题是:这是可选的,当选择隐藏时总是有默认值吗?

$('[type="checkbox"][name^="seeds-"]').change(function(){
  $(this).nextAll('select.selectDrop').first().toggle(this.checked);
});

感谢&#39; s Rajaprabhu Aravindasamy这个js

DEMO

1 个答案:

答案 0 :(得分:2)

尝试,

$('[type="checkbox"][name^="seeds-"]').change(function(){
 var selectElem = $(this).nextAll('select.selectDrop').first();
 selectElem.toggle(this.checked);
 if(!this.checked){ selectElem.val(""); }
});

DEMO