2016-07-06 57 views
0

我是Javascript和Jquery中的新成员,所以出现问题。Javascript条件 - 如果复选框条件为true,则显示元素

这是一个代码,用于检查是否选中了三个复选框,最后 - 条件 - 如果所有这三个选项都被选中,则显示一个html元素。

$('input[class^="class"]').click(function() { var $this = $(this); 
    if ($this.is(".class1")) { 
     if ($this.is(":checked")) { 
      $(".class1").not($this).prop({ disabled: true, checked: false }); 
      $(".class").prop("checked", true); 
      setTimeout(function() { $('#2').click(); }, 1000); //oncheck moves to the next question 
      var questionOne = 1; 
     } else { 
      $(".class1").prop("disabled", false); 
     } 
    } 

    if($this.is(".class2")) { 
     if ($this.is(":checked")) { 
      $(".class2").not($this).prop({ disabled: true, checked: false }); 
      $(".class").prop("checked", true); 
      setTimeout(function() { $('#3').click(); }, 1000); 
      var questionTwo = 1; 
     } else { 
      $(".class2").prop("disabled", false); 
     } 
    } 

    if($this.is(".class3")) { 
     if ($this.is(":checked")) { 
      $(".class3").not($this).prop({ disabled: true, checked: false }); 
      $(".class").prop("checked", true); 
      setTimeout(function() { $('#4').click(); }, 1000); 
      var questionThree = 1; 
     } else { 
      $(".class3").prop("disabled", false); 
     } 
    } 

if(questionOne = 1 && questionTwo = 1 && questionThree = 1) { alert("alert on Page load"); }            
}); 

我想问题是在设置变量或在最后一个条件。

提前致谢! 最好的问候, 乔尼

+0

'如果(questionOne = 1 ...' - 使用'='* *分配值1到可变要* *试验该值使用'==='或'=='。 – nnnnnn

回答

1

更改您的最后一个条件检查:

if(questionOne === 1 && questionTwo === 1 && questionThree === 1) { 
    alert("alert on Page load"); 
} 

你可以在http://www.w3schools.com/js/js_comparisons.asp大约在JavaScript相比一些基本知识

1

兄弟,做ü意味着像?

HTML:

<input type="checkbox" name="get" class="check1"> 
<input type="checkbox" name="get2" class="check2"> 
<input type="checkbox" name="get3" class="check3"> 

Jquery的:

$(document).ready(function(){ 
    var checkAndShow = function(){ 
     return ($('.check1').is(':checked')&&$('.check2').is(':checked')&&$('.check3').is(':checked')); 
    } 

    $('.check1,.check2,.check3').on('change', function(e){ 
     if(checkAndShow()){ 
      //show your div here 
     } 
     else{ 
      //hide your div here 
     } 
    }); 
}); 
+0

是的,非常感谢,这正是我所需要的。 – joni