Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 选择“禁止出错列表”_Javascript_Jquery_Select_Onchange - Fatal编程技术网

Javascript 选择“禁止出错列表”

Javascript 选择“禁止出错列表”,javascript,jquery,select,onchange,Javascript,Jquery,Select,Onchange,我正在尝试实现以下功能: 如果从第一个或第二个列表中选择[data select=“Practice”],[data select=“law”],则禁用第三个和第四个列表[data select=“other”] 或 如果从第三或第四个列表中选择[data set=“other”],则禁用除所选列表之外的所有[data set=“default”] 我遇到的问题是,如果您更改第一个和第二个[data select=“Practice”],[data select=“law”],然后将其中一个更改

我正在尝试实现以下功能:

如果从第一个或第二个列表中选择[data select=“Practice”],[data select=“law”],则禁用第三个和第四个列表[data select=“other”]

如果从第三或第四个列表中选择[data set=“other”],则禁用除所选列表之外的所有[data set=“default”]

我遇到的问题是,如果您更改第一个和第二个[data select=“Practice”],[data select=“law”],然后将其中一个更改回默认值,则即使前两个选项中的一个仍然处于选中状态,选择[data select=“other”]也会被禁用

HTML


我已将您的javascript修改为:

$('select').change(function() {
    var self = $(this);
    var myVal = self.val();
    var selPractice = $('[data-select="practise"]');
    var selLaw = $('[data-select="law"]');
    var defaultSelect = $('[data-set="default"]');
    var otherSelect = $('[data-select="other"]');
    var mySelect = self.attr('data-select');

    if(mySelect === 'practise' || mySelect === 'law') {
        var others = self.closest('fieldset').find(otherSelect);
        if (selPractice.val() == '' && selLaw.val()=='' ) {
            others.prop('disabled',false);
        }
        else {
            others.prop('disabled',true);
        }

    } else if (mySelect === 'other') {
        var others = self.closest('fieldset').find(defaultSelect).not(self);
        compare();
    }
    function compare() {        
        if (myVal !=='') {
            others.prop('disabled',true);
        } else {
            others.prop('disabled',false);
        }
    }
});

您还可以在以下位置找到可用的小提琴:

试试这个:
compare($myVal)
函数compare($myVal){
当您禁用时,可以使用带有空字符串的
val()
清除值上述任何一项都不起作用:(我可以看到它在小提琴中工作,但出于某种原因,它对我不起作用。看起来我在获取变量selPractice和selLawMichal的值时遇到了问题,刚刚意识到它不起作用,因为我在页面上有两个过滤器实例。无论如何,在定位selPractice.val()时要更具体一些??谢谢谢谢你的帮助,你的版本是正确的,所以打绿色勾
$('select').change(function() {
    var $this = $(this);
    var $myVal = $this.val();
    var $defaultSelect = ('[data-set="default"]');
    var $otherSelect = ('[data-select="other"]');
    var $mySelect = $this.attr('data-select');

    if($mySelect === 'practise' || $mySelect === 'law') {
        var $others = $this.closest('fieldset').find($otherSelect);
        compare();
    } else if ($mySelect === 'other') {
        var $others = $this.closest('fieldset').find($defaultSelect).not($this);
        compare();
    }
    function compare() {
        if ($myVal !== '') {
            $others.prop('disabled',true);
        } else {
            $others.prop('disabled',false);
        }
    }
});
$('select').change(function() {
    var self = $(this);
    var myVal = self.val();
    var selPractice = $('[data-select="practise"]');
    var selLaw = $('[data-select="law"]');
    var defaultSelect = $('[data-set="default"]');
    var otherSelect = $('[data-select="other"]');
    var mySelect = self.attr('data-select');

    if(mySelect === 'practise' || mySelect === 'law') {
        var others = self.closest('fieldset').find(otherSelect);
        if (selPractice.val() == '' && selLaw.val()=='' ) {
            others.prop('disabled',false);
        }
        else {
            others.prop('disabled',true);
        }

    } else if (mySelect === 'other') {
        var others = self.closest('fieldset').find(defaultSelect).not(self);
        compare();
    }
    function compare() {        
        if (myVal !=='') {
            others.prop('disabled',true);
        } else {
            others.prop('disabled',false);
        }
    }
});