2014-02-25 34 views
0

嗨我有以下代碼,但無法讓它工作。我希望在複選框被選中後顯示鏈接。如果2複選框的勾選執行jquery函數

HTML:

<div><input id="dp-terms" name="terms" type="checkbox" value="1" /> I have read and agree  to your terms and conditions</div> 
<div><input id="dp-artwork" name="artwork" type="checkbox" value="1" /> I have read and undertand your artwork guide</div> 
<div id="show-continue" style="display:none;"><a href="#">Next ></a></div> 

jQuery的;

$(document).ready(function(){ 
    $("#dp-terms, #dp-artwork").change(function() {  
     if ($("input#dp-terms").checked && ("input#dp-artwork").checked) { 
      $("#show-continue").show(); 
     } else { 
      $("#show-continue").hide(); 
     }; 
    }); 
}); 

http://jsfiddle.net/dannyj6/EJZuC/

回答

1

checked是DOM節點上的屬性,而不是jQuery對象,所以你必須寫:

$("input#dp-terms")[0].checked 

你也忘了美元符號,當你嘗試檢查第二個複選框。這裏是工作代碼:

$(document).ready(function(){ 
    $("#dp-terms, #dp-artwork").change(function() {  
     if ($("input#dp-terms")[0].checked && $("input#dp-artwork")[0].checked) { 
      $("#show-continue").show(); 
     } else { 
      $("#show-continue").hide(); 
     }; 
    }); 
}); 

小提琴:http://jsfiddle.net/EJZuC/2/

0

嘗試

$(document).ready(function(){ 
    $("#dp-terms, #dp-artwork").click(function() {  
     alert('in'); 
     if ($("input#dp-terms").is(":checked") && $("input#dp-artwork").is(":checked")) { 
      $("#show-continue").show(); 
     } else { 
      $("#show-continue").hide(); 
     }; 
    }); 
}); 

小提琴:http://jsfiddle.net/EJZuC/3/

2
$(document).ready(function() { 
    $("#dp-terms, #dp-artwork").change(function() { 
     $("#show-continue").css('display', ($("#dp-terms").is(':checked') && $("#dp-artwork").is(':checked')) ? '' : 'none'); 
    }); 
}); 

jsFiddle example

相關問題