• <i id='0ZDfG'><tr id='0ZDfG'><dt id='0ZDfG'><q id='0ZDfG'><span id='0ZDfG'><b id='0ZDfG'><form id='0ZDfG'><ins id='0ZDfG'></ins><ul id='0ZDfG'></ul><sub id='0ZDfG'></sub></form><legend id='0ZDfG'></legend><bdo id='0ZDfG'><pre id='0ZDfG'><center id='0ZDfG'></center></pre></bdo></b><th id='0ZDfG'></th></span></q></dt></tr></i><div id='0ZDfG'><tfoot id='0ZDfG'></tfoot><dl id='0ZDfG'><fieldset id='0ZDfG'></fieldset></dl></div>

      <bdo id='0ZDfG'></bdo><ul id='0ZDfG'></ul>

      <tfoot id='0ZDfG'></tfoot>

      <small id='0ZDfG'></small><noframes id='0ZDfG'>

        <legend id='0ZDfG'><style id='0ZDfG'><dir id='0ZDfG'><q id='0ZDfG'></q></dir></style></legend>
      1. Jquery如果选中单选按钮

        Jquery If radio button is checked(Jquery如果选中单选按钮)
        • <bdo id='QTyhA'></bdo><ul id='QTyhA'></ul>
              <tbody id='QTyhA'></tbody>

            <i id='QTyhA'><tr id='QTyhA'><dt id='QTyhA'><q id='QTyhA'><span id='QTyhA'><b id='QTyhA'><form id='QTyhA'><ins id='QTyhA'></ins><ul id='QTyhA'></ul><sub id='QTyhA'></sub></form><legend id='QTyhA'></legend><bdo id='QTyhA'><pre id='QTyhA'><center id='QTyhA'></center></pre></bdo></b><th id='QTyhA'></th></span></q></dt></tr></i><div id='QTyhA'><tfoot id='QTyhA'></tfoot><dl id='QTyhA'><fieldset id='QTyhA'></fieldset></dl></div>

              <tfoot id='QTyhA'></tfoot>
                • <legend id='QTyhA'><style id='QTyhA'><dir id='QTyhA'><q id='QTyhA'></q></dir></style></legend>

                  <small id='QTyhA'></small><noframes id='QTyhA'>

                  本文介绍了Jquery如果选中单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

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

                  I have these 2 radio buttons at the moment so that the user can decide whether they need postage included in the price or not:

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

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

                  I need to use Jquery to check if the 'yes' radio button is checked, and if it is, do an append function. Could someone tell me how I'd do this please?

                  感谢您的帮助

                  我已将我的代码更新为此,但它不起作用.我做错了吗?

                  I've updated my code to this, but it's not working. Am I doing something wrong?

                  <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:

                  Or, the above - again - using a little less superfluous 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:

                  Revised (improved-some) 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

                  JS Fiddle 演示.

                  此外,还有一个温和的更新(因为我正在编辑以包含片段以及 JS Fiddle 链接),以便用 包装 元素<label>s - 允许单击文本以更新相关 <input/> - 并更改创建要附加的内容的方式:

                  And, further, a mild update (since I was editing to include Snippets as well as the JS Fiddle links), in order to wrap the <input /> elements with <label>s - allow for clicking the text to update the relevant <input /> - and changing the means of creating the content to append:

                  var appended = $('<div />', {
                    'id': 'appended',
                    'text': 'Appended content'
                  });
                  $('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/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>

                  JS Fiddle 演示.

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

                  Also, if you only need to show content depending on which element is checked by the user, a slight update that will toggle visibility using an explicit show/hide:

                  // 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();

                  <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>

                  最后,只使用 CSS:

                  And, finally, using just 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;
                  }

                  <!-- 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>

                  JS Fiddle 演示.

                  参考资料:

                  • CSS:
                    • :checked 选择器.
                    • CSS 属性选择器.
                    • 通用兄弟 (~) 组合子.
                    • appendTo().
                    • attribute-equals 选择器.
                    • change().
                    • :checked 选择器.
                    • filter().
                    • is().
                    • :radio 选择器.
                    • remove().
                    • text().
                    • toggle().
                    • val().

                    这篇关于Jquery如果选中单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                    本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  keeping radio buttons checked after form submit(表单提交后保持选中单选按钮)
                  Styling radio button - not work in IE and Firefox(样式单选按钮 - 在 IE 和 Firefox 中不起作用)
                  Bootstrap radio button quot;checkedquot; flag(Bootstrap 单选按钮“已选中旗帜)
                  Toggle HTML radio button by clicking its label(通过单击标签来切换 HTML 单选按钮)
                  Javascript how to change radio button label text?(Javascript如何更改单选按钮标签文本?)
                  How can I automatically select specific radio buttons with Greasemonkey?(如何使用 Greasemonkey 自动选择特定的单选按钮?)
                • <small id='C4d4d'></small><noframes id='C4d4d'>

                  • <bdo id='C4d4d'></bdo><ul id='C4d4d'></ul>

                    1. <legend id='C4d4d'><style id='C4d4d'><dir id='C4d4d'><q id='C4d4d'></q></dir></style></legend>
                          <i id='C4d4d'><tr id='C4d4d'><dt id='C4d4d'><q id='C4d4d'><span id='C4d4d'><b id='C4d4d'><form id='C4d4d'><ins id='C4d4d'></ins><ul id='C4d4d'></ul><sub id='C4d4d'></sub></form><legend id='C4d4d'></legend><bdo id='C4d4d'><pre id='C4d4d'><center id='C4d4d'></center></pre></bdo></b><th id='C4d4d'></th></span></q></dt></tr></i><div id='C4d4d'><tfoot id='C4d4d'></tfoot><dl id='C4d4d'><fieldset id='C4d4d'></fieldset></dl></div>
                        • <tfoot id='C4d4d'></tfoot>

                              <tbody id='C4d4d'></tbody>