jQuery – 禁用所选的选项

前端之家收集整理的这篇文章主要介绍了jQuery – 禁用所选的选项前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
需要使用jQuery在选择框中禁用已选择的选项。我想它像 asmselect灰色。

测试我的例子here

  1. //JS
  2. $("#theSelect").change(function(){
  3. var value = $("#theSelect option:selected").val();
  4. var theDiv = $(".is" + value);
  5.  
  6. theDiv.slideDown().removeClass("hidden");
  7. });
  8.  
  9.  
  10. $("div a.remove").click(function () {
  11. $(this).parent().slideUp(function() { $(this).addClass("hidden"); });
  12. });
  13.  
  14. //HTML
  15. <body>
  16. <div class="selectContainer">
  17. <select id="theSelect">
  18. <option value="">- Select -</option>
  19. <option value="Patient">Patient</option>
  20. <option value="Physician">Physician</option>
  21. <option value="Nurse">Nurse</option>
  22. </select>
  23. </div>
  24. <div class="hidden isPatient">Patient <a href="#" class="remove" rel="Patient">remove</a></div>
  25. <div class="hidden isPhysician">Physician <a href="#" class="remove" rel="Patient">remove</a></div>
  26. <div class="hidden isNurse">Nurse <a href="#" class="remove" rel="Patient">remove</a></div>
  27. </body>​

UPDATED:这是finished solution.感谢Patrick和Simen。

解决方法@H_301_10@
将此行添加到您的更改事件处理程序
  1. $("#theSelect option:selected").attr('disabled','disabled')
  2. .siblings().removeAttr('disabled');

这将禁用所选选项,并启用任何先前禁用的选项。

编辑:

如果你不想重新启用以前的,只需删除这部分行:

  1. .siblings().removeAttr('disabled');

编辑:

http://jsfiddle.net/pd5Nk/1/

要在点击删除时重新启用,请将其添加到您的点击处理程序。

  1. $("#theSelect option[value=" + value + "]").removeAttr('disabled');

猜你在找的jQuery相关文章