无法弄清楚为什么我的javascript无法正常工作

时间:2015-04-28 13:42:26

标签: javascript

问题是我有一个javascript,它会查看几个文本字段并获取每个文本字段的值。将它们添加到一起,并在我的剪贴簿文本字段中提供总计。我想要做的是说如果设置是A B C或D,如果设置的值为空,则执行其他操作。它似乎不想工作任何帮助将不胜感激。

$(document).ready(function(){

  $('.scrap').click(function() {
    var weld_scrap = $('.weld').val();
    var dim_scrap = $('.dim').val();
    var visual_scrap = $('.visual').val();
    var adjust_scrap = $('.adjust').val();
    var setup = $('.setup').val();
    console.log(setup)
    var sum = parseInt(weld_scrap) + parseInt(dim_scrap) + parseInt(visual_scrap) + parseInt(adjust_scrap) + parseInt(setup);
    var other = parseInt(weld_scrap) + parseInt(dim_scrap) + parseInt(visual_scrap) + parseInt(adjust_scrap);
    var A = '10';
    var B = '10';
    var C = '10';
    var D = '10';
    if (setup == 'A') {
      setup = A;
         $('.scrap').val(sum);

    }else if (setup == 'B') {
      setup = B;
         $('.scrap').val(sum);

    }else if (setup == 'C') {
      setup = C;
         $('.scrap').val(sum);

    }else if (setup == 'D') {
      setup = D;
          $('.scrap').val(sum);
    }else if (setup == '') {
       $('.scrap').val(other);

    }
  });
});

如果设置为空白,我也试过这个

$(document).ready(function(){

  $('.scrap').click(function() {
    var weld_scrap = $('.weld').val();
    var dim_scrap = $('.dim').val();
    var visual_scrap = $('.visual').val();
    var adjust_scrap = $('.adjust').val();
    var setup = $('.setup').val();
    console.log(setup)
    var sum = parseInt(weld_scrap) + parseInt(dim_scrap) + parseInt(visual_scrap) + parseInt(adjust_scrap) + parseInt(setup);
    var other = parseInt(weld_scrap) + parseInt(dim_scrap) + parseInt(visual_scrap) + parseInt(adjust_scrap);
    var A = '10';
    var B = '10';
    var C = '10';
    var D = '10';
    if (setup == 'A') {
      setup = A;
         $('.scrap').val(sum);

    }else if (setup == 'B') {
      setup = B;
         $('.scrap').val(sum);

    }else if (setup == 'C') {
      setup = C;
         $('.scrap').val(sum);

    }else if (setup == 'D') {
      setup = D;
          $('.scrap').val(sum);
    }else if (setup === null) {
       $('.scrap').val(other);

    }
  });
});

这也是

$(document).ready(function(){

  $('.scrap').click(function() {
    var weld_scrap = $('.weld').val();
    var dim_scrap = $('.dim').val();
    var visual_scrap = $('.visual').val();
    var adjust_scrap = $('.adjust').val();
    var setup = $('.setup').val();
    console.log(setup)
    var sum = parseInt(weld_scrap) + parseInt(dim_scrap) + parseInt(visual_scrap) + parseInt(adjust_scrap) + parseInt(setup);
    var other = parseInt(weld_scrap) + parseInt(dim_scrap) + parseInt(visual_scrap) + parseInt(adjust_scrap);
    var A = '10';
    var B = '10';
    var C = '10';
    var D = '10';
    if (setup == 'A') {
      setup = A;
         $('.scrap').val(sum);

    }else if (setup == 'B') {
      setup = B;
         $('.scrap').val(sum);

    }else if (setup == 'C') {
      setup = C;
         $('.scrap').val(sum);

    }else if (setup == 'D') {
      setup = D;
          $('.scrap').val(sum);
    }else if (setup == '0') {
       $('.scrap').val(other);

    }
  });
});

这也是......

$(document).ready(function(){

  $('.scrap').click(function() {
    var weld_scrap = $('.weld').val();
    var dim_scrap = $('.dim').val();
    var visual_scrap = $('.visual').val();
    var adjust_scrap = $('.adjust').val();
    var setup = $('.setup').val();
    console.log(setup)
    var sum = parseInt(weld_scrap) + parseInt(dim_scrap) + parseInt(visual_scrap) + parseInt(adjust_scrap) + parseInt(setup);
    var other = parseInt(weld_scrap) + parseInt(dim_scrap) + parseInt(visual_scrap) + parseInt(adjust_scrap);
    var A = '10';
    var B = '10';
    var C = '10';
    var D = '10';
    if (setup == 'A') {
      setup = A;
         $('.scrap').val(sum);

    }else if (setup == 'B') {
      setup = B;
         $('.scrap').val(sum);

    }else if (setup == 'C') {
      setup = C;
         $('.scrap').val(sum);

    }else if (setup == 'D') {
      setup = D;
          $('.scrap').val(sum);
    }else if (setup === undefined) {
       $('.scrap').val(other);

    }
  });
});

2 个答案:

答案 0 :(得分:0)

您可以从一开始就检查该值是否为空,否则请检查其是否等于' A'或者' B' ....

if( setup.match(/\S/)){
  if (setup == 'A') {
    setup = A;
       $('.scrap').val(sum);

  }else if (setup == 'B') {
    setup = B;
       $('.scrap').val(sum);

  }else if (setup == 'C') {
    setup = C;
       $('.scrap').val(sum);

  }else if (setup == 'D') {
    setup = D;
        $('.scrap').val(sum);
  }
}else{
  // will come here when there is an empty value 
  $('.scrap').val(other);
}

答案 1 :(得分:-1)

检查您是否导入了正确的jquery标头。虽然你可以使用.on()方法并检查它.. 使用console.log()进行开发时,从开始一步一步检查是很好的。

$( ".scrap" ).on( "click", function() {
    // your stuff here 
   );
});