Jquery如果选中单选按钮

Dan*_*l H 142 html jquery radio-button

可能重复:
选中特定单选按钮的检查

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

<input type="radio" id="postageyes" name="postage" value="Yes" /> Yes
<input type="radio" id="postageno" name="postage" value="No" /> No
Run Code Online (Sandbox Code Playgroud)

我需要使用Jquery来检查是否选中了'yes'单选按钮,如果是,则执行追加功能.有人可以告诉我,我该怎么做?

谢谢你的帮助

编辑:

我已将我的代码更新为此,但它无效.难道我做错了什么?

<script type='text/javascript'>
// <![CDATA[
jQuery(document).ready(function(){

$('input:radio[name="postage"]').change(function(){
    if($(this).val() == 'Yes'){
       alert("test");
    }
});

});

// ]]>
</script>
Run Code Online (Sandbox Code Playgroud)

Dav*_*mas 270

$('input:radio[name="postage"]').change(
    function(){
        if ($(this).is(':checked') && $(this).val() == 'Yes') {
            // append goes here
        }
    });
Run Code Online (Sandbox Code Playgroud)

或者,上面-再-用一点点多余的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
        }
    });
Run Code Online (Sandbox Code Playgroud)

修改(改进一些)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();
        }
    });
Run Code Online (Sandbox Code Playgroud)

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();
  }
});
Run Code Online (Sandbox Code Playgroud)
<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
Run Code Online (Sandbox Code Playgroud)

JS小提琴演示.

此外,一个温和的更新(因为我正在编辑包括Snippets以及JS Fiddle链接),为了<input /><label>s 包装元素- 允许单击文本以更新相关<input />- 并更改创建的方法要追加的内容:

var appended = $('<div />', {
  'id': 'appended',
  'text': 'Appended content'
});
$('input:radio[name="postage"]').change(function() {
  if ($(this).val() == 'Yes') {
    $(appended).appendTo('body');
  } else {
    $(appended).remove();
  }
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>
  <input type="radio" id="postageyes" name="postage" value="Yes" />Yes</label>
<label>
  <input type="radio" id="postageno" name="postage" value="No" />No</label>
Run Code Online (Sandbox Code Playgroud)

JS小提琴演示.

此外,如果您只需要根据用户选中的元素显示内容,则会稍微更新,以使用显式显示/隐藏来切换可见性:

// caching a reference to the dependant/conditional content:
var conditionalContent = $('#conditional'),
    // caching a reference to the group of inputs, since we're using that
    // same group twice:
    group = $('input[type=radio][name=postage]');

// binding the change event-handler:
group.change(function() {
  // toggling the visibility of the conditionalContent, which will
  // be shown if the assessment returns true and hidden otherwise:
  conditionalContent.toggle(group.filter(':checked').val() === 'Yes');
  // triggering the change event on the group, to appropriately show/hide
  // the conditionalContent on page-load/DOM-ready:
}).change();
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>
  <input type="radio" id="postageyes" name="postage" value="Yes" />Yes</label>
<label>
  <input type="radio" id="postageno" name="postage" value="No" />No</label>
<div id="conditional">
  <p>This should only show when the 'Yes' radio &lt;input&gt; element is checked.</p>
</div>
Run Code Online (Sandbox Code Playgroud)

最后,使用CSS:

/* setting the default of the conditionally-displayed content
to hidden: */
#conditional {
  display: none;
}

/* if the #postageyes element is checked then the general sibling of
that element, with the id of 'conditional', will be shown: */
#postageyes:checked ~ #conditional {
  display: block;
}
Run Code Online (Sandbox Code Playgroud)
<!-- note that the <input> elements are now not wrapped in the <label> elements,
in order that the #conditional element is a (subsequent) sibling of the radio
<input> elements: -->
<input type="radio" id="postageyes" name="postage" value="Yes" />
<label for="postageyes">Yes</label>
<input type="radio" id="postageno" name="postage" value="No" />
<label for="postageno">No</label>
<div id="conditional">
  <p>This should only show when the 'Yes' radio &lt;input&gt; element is checked.</p>
</div>
Run Code Online (Sandbox Code Playgroud)

JS小提琴演示.

参考文献:

  • 无需查看是否已检查.否则它永远不会有价值.浪费资源! (3认同)

Sha*_*oli 20

试试这个

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


Mrc*_*ief 10

像这样的东西:

if($('#postageyes').is(':checked')) {
// do stuff
}
Run Code Online (Sandbox Code Playgroud)

  • jQuery对象总是很简洁.您可能想要使用`$(...).length`. (2认同)

She*_*hef 7

$('input:radio[name="postage"]').change(function(){
    if($(this).val() === 'Yes'){
       // append stuff
    }
});
Run Code Online (Sandbox Code Playgroud)

这将监听单选按钮上的更改事件.在用户点击时Yes,事件将触发,您将能够将任何您喜欢的内容附加到DOM.


Phi*_*hil 6

if($('#test2').is(':checked')) {
    $(this).append('stuff');
} 
Run Code Online (Sandbox Code Playgroud)