jquery如果单选button被选中

可能重复:
检查特定的单选button

我现在有这两个单选button,以便用户可以决定是否需要包含在价格中的邮资:

<input type="radio" id="postageyes" name="postage" value="Yes" /> Yes <input type="radio" id="postageno" name="postage" value="No" /> No 

我需要使用Jquery检查是否选中“是”单选button,如果是,请执行附加function。 有人能告诉我怎么做这个吗?

谢谢你的帮助

编辑:

我已经更新了我的代码,但它不工作。 难道我做错了什么?

 <script type='text/javascript'> // <![CDATA[ jQuery(document).ready(function(){ $('input:radio[name="postage"]').change(function(){ if($(this).val() == 'Yes'){ alert("test"); } }); }); // ]]> </script> 
 $('input:radio[name="postage"]').change( function(){ if ($(this).is(':checked') && $(this).val() == 'Yes') { // append goes here } }); 

或者,再上面 – 使用less一些多余的jQuery:

 $('input:radio[name="postage"]').change( function(){ if (this.checked && this.value == 'Yes') { // note that, as per comments, the 'changed' // <input> will *always* be checked, as the change // event only fires on checking an <input>, not // on un-checking it. // append goes here } }); 

修订(改进了一些)jQuery:

 // defines a div element with the text "You're appendin'!" // assigns that div to the variable 'appended' var appended = $('<div />').text("You're appendin'!"); // assigns the 'id' of "appended" to the 'appended' element appended.id = 'appended'; // 1. selects '<input type="radio" />' elements with the 'name' attribute of 'postage' // 2. assigns the onChange/onchange event handler $('input:radio[name="postage"]').change( function(){ // checks that the clicked radio button is the one of value 'Yes' // the value of the element is the one that's checked (as noted by @shef in comments) if ($(this).val() == 'Yes') { // appends the 'appended' element to the 'body' tag $(appended).appendTo('body'); } else { // if it's the 'No' button removes the 'appended' element. $(appended).remove(); } }); 
 var appended = $('<div />').text("You're appendin'!"); appended.id = 'appended'; $('input:radio[name="postage"]').change(function() { if ($(this).val() == 'Yes') { $(appended).appendTo('body'); } else { $(appended).remove(); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> <input type="radio" id="postageyes" name="postage" value="Yes" />Yes <input type="radio" id="postageno" name="postage" value="No" />No 

尝试这个

 if($("input:radio[name=postage]").is(":checked")){ //Code to append goes here } 

像这样的东西:

 if($('#postageyes').is(':checked')) { // do stuff } 
 $('input:radio[name="postage"]').change(function(){ if($(this).val() === 'Yes'){ // append stuff } }); 

这将监听单选button上的更改事件。 当用户点击Yes ,事件就会触发,你可以将任何你喜欢的东西添加到DOM。

 if($('#test2').is(':checked')) { $(this).append('stuff'); } 
 $("input").bind('click', function(e){ if ($(this).val() == 'Yes') { $("body").append('whatever'); } }); 

尝试这个:

 if ( jQuery('#postageyes').is(':checked') ){ ... } 
 jQuery('input[name="inputName"]:checked').val()