禁用选择功能Jquery 1.9(Disable select function Jquery 1.9)

编程入门 行业动态 更新时间:2024-10-27 18:32:17
禁用选择功能Jquery 1.9(Disable select function Jquery 1.9)

以下脚本适用于除1.9(ahhh)之外的所有Jquery版本。 这很简单我有5个下拉列表,每个列表中有20个相同的选项。 每次他们选择一个选项时,它都被禁用,无法在下一个列表中选择。 在任何人提到使用复选框功能的想法之前我已经这样做了,并且效果很好,但是楼上的大人物决定他们希望我重写这个脚本更好。

$("select").change(function () { var $this = $(this); var prevVal = $this.data("prev"); var otherSelects = $("select").not(this); otherSelects.find("option[value=" + $(this).val('') + "]").prop('disabled', true); if (prevVal) { otherSelects.find("option[value=" + prevVal + "]").prop('disabled', false); } $this.data("prev", $this.val()); }); function clearForm(form) { // iterate over all of the inputs for the form // element that was passed in $(':input', form).each(function() { var type = this.type; var tag = this.tagName.toLowerCase(); // normalize case // it's ok to reset the value attr of text inputs, // password inputs, and textareas if (type == 'text' || type == 'password' || tag == 'textarea') this.value = ""; // checkboxes and radios need to have their checked state cleared // but should *not* have their 'value' changed else if (type == 'checkbox' || type == 'radio') this.checked = false; // select elements need to have their 'selectedIndex' property set to -1 // (this works for both single and multiple select elements) else if (tag == 'select') this.selectedIndex = -1; }); };

The following script works in all versions of Jquery except 1.9 (ahhh). It's very simple I have 5 dropdown list with the same 20 options in each list. Everytime they choose one option it is disabled and can not be chosen in the next list. And before anyone mentions the idea of using a checkbox function I did this already, and it worked great, but the big man upstair decided they want me to rewrite this script preferable.

$("select").change(function () { var $this = $(this); var prevVal = $this.data("prev"); var otherSelects = $("select").not(this); otherSelects.find("option[value=" + $(this).val('') + "]").prop('disabled', true); if (prevVal) { otherSelects.find("option[value=" + prevVal + "]").prop('disabled', false); } $this.data("prev", $this.val()); }); function clearForm(form) { // iterate over all of the inputs for the form // element that was passed in $(':input', form).each(function() { var type = this.type; var tag = this.tagName.toLowerCase(); // normalize case // it's ok to reset the value attr of text inputs, // password inputs, and textareas if (type == 'text' || type == 'password' || tag == 'textarea') this.value = ""; // checkboxes and radios need to have their checked state cleared // but should *not* have their 'value' changed else if (type == 'checkbox' || type == 'radio') this.checked = false; // select elements need to have their 'selectedIndex' property set to -1 // (this works for both single and multiple select elements) else if (tag == 'select') this.selectedIndex = -1; }); };

最满意答案

http://jquery.com/upgrade-guide/1.9/#attr-versus-prop-

但是, 当使用类似“input [value = abc]”的选择器时 ,它应始终通过value属性进行选择,而不是用户对属性所做的任何更改,例如从键入文本输入中进行选择。 从jQuery 1.9开始,这种行为正确且一致。 早期版本的jQuery有时会在他们应该使用该属性时使用该属性。 大段引用

换一种说法”

otherSelects.find("option[value=" + $(this).val('') + "]").attr('disabled', 'disabled') otherSelects.find("option[value=" + prevVal + "]").removeAttr('disabled');

I thought I add the answer to this question if anyone ever needs to use this type of script:

$("select").change(function () { var $this = $(this); var prevVal = $this.data("prev"); var otherSelects = $("select").not(this); var thisVal = $(this).val(); otherSelects.find("option:contains(" + thisVal + ")").attr('disabled', true); if (prevVal) { otherSelects.find("option:contains(" + prevVal + ")").attr('disabled', false); } $this.data("prev", $this.val()); });

If you want to see it in action it's here: http://jsfiddle.net/abigaild12/UQA3X/1/

更多推荐

本文发布于:2023-08-07 19:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465918.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:功能   Jquery   function   select   Disable

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!