How to check a radio button with jQuery?

前端 未结 30 2083
独厮守ぢ
独厮守ぢ 2020-11-22 08:12

I try to check a radio button with jQuery. Here\'s my code:

相关标签:
30条回答
  • 2020-11-22 08:49

    Yes, it worked for me like a way:

    $("#radio_1").attr('checked', 'checked');
    
    0 讨论(0)
  • 2020-11-22 08:50

    Try this.

    In this example, I'm targeting it with its input name and value

    $("input[name=background][value='some value']").prop("checked",true);
    

    Good to know: in case of multi-word value, it will work because of apostrophes, too.

    0 讨论(0)
  • 2020-11-22 08:50

    Some times above solutions do not work, then you can try below:

    jQuery.uniform.update(jQuery("#yourElementID").attr('checked',true));
    jQuery.uniform.update(jQuery("#yourElementID").attr('checked',false));
    

    Another way you can try is:

    jQuery("input:radio[name=yourElementName]:nth(0)").attr('checked',true);
    
    0 讨论(0)
  • 2020-11-22 08:55

    I use this code:

    I'm sorry for English.

    var $j = jQuery.noConflict();
    
    $j(function() {
        // add handler
        $j('#radio-1, #radio-2').click(function(){
    
            // find all checked and cancel checked
            $j('input:radio:checked').prop('checked', false);
    
            // this radio add cheked
            $j(this).prop('checked', true);
        });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <fieldset class="section">
      <legend>Radio buttons</legend>
      <label>
        <input type="radio" id="radio-1" checked>
        Option one is this and that&mdash;be sure to include why it's great
      </label>
      <br>
      <label>
        <input type="radio" id="radio-2">
        Option two can be something else
      </label>
    </fieldset>

    0 讨论(0)
  • 2020-11-22 08:55

    I used jquery-1.11.3.js

    Basic Enable & disable

    Tips 1: (Radio button type common Disable & Enable)

    $("input[type=radio]").attr('disabled', false);
    $("input[type=radio]").attr('disabled', true); 
    

    Tips 2: ( ID selector Using prop() or attr())

    $("#paytmradio").prop("checked", true);
    $("#sbiradio").prop("checked", false);
    
    jQuery("#paytmradio").attr('checked', 'checked'); // or true this won't work
    jQuery("#sbiradio").attr('checked', false);
    

    Tips 3: ( Class selector Using prop() or arrt())

    $(".paytm").prop("checked", true);
    $(".sbi").prop("checked", false);
    
    jQuery(".paytm").attr('checked', 'checked'); // or true
    jQuery(".sbi").attr('checked', false);
    

    OTHER TIPS

    $("#paytmradio").is(":checked")   // Checking is checked or not
    $(':radio:not(:checked)').attr('disabled', true); // All not check radio button disabled
    
    $('input[name=payment_type][value=1]').attr('checked', 'checked'); //input type via checked
     $("input:checked", "#paytmradio").val() // get the checked value
    

    index.html

    <div class="col-md-6">      
        <label class="control-label" for="paymenttype">Payment Type <span style="color:red">*</span></label>
        <div id="paymenttype" class="form-group" style="padding-top: inherit;">
            <label class="radio-inline" class="form-control"><input  type="radio" id="paytmradio"  class="paytm" name="paymenttype" value="1" onclick="document.getElementById('paymentFrm').action='paytmTest.php';">PayTM</label>
            <label class="radio-inline" class="form-control"><input  type="radio" id="sbiradio" class="sbi" name="paymenttype" value="2" onclick="document.getElementById('paymentFrm').action='sbiTest.php';">SBI ePAY</label>
        </div>
    </div>
    
    0 讨论(0)
  • 2020-11-22 08:56

    Try This:

    $(document).ready(function(){
      $("#Id").prop("checked", true).checkboxradio('refresh');
    });
    
    0 讨论(0)
提交回复
热议问题