简体   繁体   English

js函数中的多个if / else语句用于唯一输入禁用

[英]Multiple if/else statements in js function for unique input disabling

I'm bringing this up because I'm still learning javascript. 之所以提出这一点,是因为我还在学习javascript。 I'm almost positive that this is not the correct way to write this function, but it works. 我几乎肯定这不是编写此函数的正确方法,但是它可以工作。 It's ugly as hell, but it works. 这很丑陋,但是可以。 Just from a look at the script, can you see an obvious oversight that would have made the code a lot neater and more semantic? 仅从脚本来看,您是否可以看到明显的疏忽会使代码更加整洁和语义化?

    $('#model').on('change', function(){
    var yxs = $('#model').find('option:selected').attr('data-yxs');
    var ys = $('#model').find('option:selected').attr('data-ys');
    var ym = $('#model').find('option:selected').attr('data-ym');
    var yl = $('#model').find('option:selected').attr('data-yl');
    var yxl = $('#model').find('option:selected').attr('data-yxl');
    var xs = $('#model').find('option:selected').attr('data-xs');
    var s = $('#model').find('option:selected').attr('data-s');
    var m = $('#model').find('option:selected').attr('data-m');
    var l = $('#model').find('option:selected').attr('data-l');
    var xl = $('#model').find('option:selected').attr('data-xl');
    var xxl = $('#model').find('option:selected').attr('data-xxl');
    var xxxl = $('#model').find('option:selected').attr('data-xxxl');
    var xxxxl = $('#model').find('option:selected').attr('data-xxxxl');
    if (yxs != '') {
        $('#yxs').prop('disabled', false);
    }
    else {
        $('#yxs').prop('disabled', true);
    }
    if (ys != '') {
        $('#ys').prop('disabled', false);
    }
    else {
        $('#ys').prop('disabled', true);
    }
    if (ys != '') {
        $('#ym').prop('disabled', false);
    }
    else {
        $('#ym').prop('disabled', true);
    }
    if (ys != '') {
        $('#yl').prop('disabled', false);
    }
    else {
        $('#yl').prop('disabled', true);
    }
    if (ys != '') {
        $('#yxl').prop('disabled', false);
    }
    else {
        $('#yxl').prop('disabled', true);
    }
    if (ys != '') {
        $('#xs').prop('disabled', false);
    }
    else {
        $('#xs').prop('disabled', true);
    }
    if (ys != '') {
        $('#s').prop('disabled', false);
    }
    else {
        $('#s').prop('disabled', true);
    }
    if (ys != '') {
        $('#m').prop('disabled', false);
    }
    else {
        $('#m').prop('disabled', true);
    }
    if (ys != '') {
        $('#l').prop('disabled', false);
    }
    else {
        $('#l').prop('disabled', true);
    }
    if (ys != '') {
        $('#xl').prop('disabled', false);
    }
    else {
        $('#xl').prop('disabled', true);
    }
    if (ys != '') {
        $('#xxl').prop('disabled', false);
    }
    else {
        $('#xxl').prop('disabled', true);
    }
    if (ys != '') {
        $('#xxxl').prop('disabled', false);
    }
    else {
        $('#xxxl').prop('disabled', true);
    }
    if (ys != '') {
        $('#xxxxl').prop('disabled', false);
    }
    else {
        $('#xxxxl').prop('disabled', true);
    }
});

You could try doing it this way: 您可以尝试通过以下方式进行操作:

$('#model').on('change', function(){
    var data = $(this).find('option:selected').data();

    for (var key in data) {            
        $("#" + key).prop("disabled", data[key] == '');            
    }
});

This works by grabbing all data-* attributes on the matched element (option:selected) into an object of the format: 这是通过将匹配元素(选项:selected)上的所有data- *属性捕获到以下格式的对象中而起作用的:

{
    yxs : 'some value',
    ys  : '',
    etc.
}

you can then iterate through this object using the for loop which stores the left side of the object attribute (ie: yxs, ys, etc.) into the key variable. 然后,您可以使用for循环遍历此对象,该循环将object属性的左侧(即yxs,ys等)存储到key变量中。 Within the loop you can then grab the corresponding element: 然后,您可以在循环中获取相应的元素:

$("#" + key)

and set the disabled property to be true or false based on the evaluation of the expression: 并根据表达式的求值将disabled属性设置为true或false:

data[key] == ''

which per the object example above would translate to 根据上面的对象示例将其转换为

data['ys'] == '' 

or 要么

data['yxs'] == '' 

etc. 等等

Try 尝试

$('#model').on('change', function(){
    var $selcted = $('#model').find('option:selected');

    $('#yxs').prop('disabled', $selcted.data('yxs') == '');
    $('#ys').prop('disabled', $selcted.data('ys') == '');
    $('#ym').prop('disabled', $selcted.data('ym') == '');
    $('#yl').prop('disabled', $selcted.data('yl') == '');
    $('#xs').prop('disabled', $selcted.data('xs') == '');
    $('#s').prop('disabled', $selcted.data('s') == '');
    $('#m').prop('disabled', $selcted.data('m') == '');
    $('#l').prop('disabled', $selcted.data('l') == '');
    $('#xl').prop('disabled', $selcted.data('xl') == '');
    $('#xxl').prop('disabled', $selcted.data('xxl') == '');
    $('#xxxl').prop('disabled', $selcted.data('xxxl') == '');
    $('#xxxxl').prop('disabled', $selcted.data('xxxxl') == '');
    $('#xl').prop('disabled', $selcted.data('xl') == '');
    $('#xl').prop('disabled', $selcted.data('xl') == '');
    $('#xl').prop('disabled', $selcted.data('xl') == '');
});

Another version could be 另一个版本可能是

$('#model').on('change', function(){
    var $selcted = $('#model').find('option:selected');

    function setDisabled(key){
        $('#' + key).prop('disabled', $selcted.data(key) == '');
    }

    setDisabled('yxs');
    setDisabled('ys');
    ....
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM