以下脚本适用于除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/
更多推荐
发布评论