How to check radio button is checked using JQuery?

后端 未结 8 843
猫巷女王i
猫巷女王i 2020-12-24 07:20

I have two radio buttons in one group, I want to check the radio button is checked or not using JQuery, How ?

8条回答
  •  忘掉有多难
    2020-12-24 07:41

    Given a group of radio buttons:

    
    
    

    You can test whether a specific one is checked using jQuery as follows:

    if ($("#radio1").prop("checked")) {
       // do something
    }
    
    // OR
    if ($("#radio1").is(":checked")) {
       // do something
    }
    
    // OR if you don't have ids set you can go by group name and value
    // (basically you need a selector that lets you specify the particular input)
    if ($("input[name='radioGroup'][value='1']").prop("checked"))
    

    You can get the value of the currently checked one in the group as follows:

    $("input[name='radioGroup']:checked").val()
    

提交回复
热议问题