开发者

Determine if radio buttons are checked using jQuery

开发者 https://www.devze.com 2023-03-25 14:07 出处:网络
I have the following table: <table id=\"tableID\"> <tr id=\"rowID\"> <td class=\"qCol\">

I have the following table:

<table id="tableID">
    <tr id="rowID">     
        <td class="qCol">     
          <select name="select1">
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
          </select>
        </td>      
        <td class="qCo2">     
        <!-- img here -->     
        <input type="text" name="text1" id="text1" />
        </td>      
        <td class="qCo3">     
          <select name="select2">
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
          </select>   
        </td>      
        <td class="qCo4">     
        <!-- radio buttons here -->   
           <input t开发者_运维问答ype='radio' name='name' value='1'>  
           <input type='radio' name='name' value='2'>
           <input type='radio' name='name' value='3'>
        </td>      
        <td class="qCo5">     
         <select name="select3">
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
                    </select>   
        </td>     
        <td class="qCo6">     
        <!-- hidden validation image here -->     
        </td> 
    <tr> 
</table>

and I have the following jQuery

$("#rowID input[type=text]").blur(function () {

    if ($(this).children("input:radio").is(':checked')) {
        alert("yes");
    } else {
        alert("no");
    }

});

however regardless of whether one of the radio buttons is selected I'm still getting the alert "no". Now I know I can see the name of the radio button group but in certain situations I won't be able to due to GUIDs - what's the best method or way to work through the dom starting at the onblur event of the textbox on the same row to determin if one of the radio buttons is checked.


Your problem is because $(this) is referring to the textbox and not the rowID.

To fix do this:

$("#rowID input[type=text]").blur(function () {

    var checkedRadios = $('#rowID').find('input:radio:checked');

    if (checkedRadios.length > 0) {
        alert("yes");
    } else {
        alert("no");
    }

});


You're using a text box selector and binding to their blur events, and within that event handler, $(this) refers to the text box itself. Since you don't have any children of the text box, it never finds any radio buttons.


$("#rowID input[type=text]").blur(function () { //ect...

$(this) insude the function will refere to your text inputs.

you need to select type=radio.

0

精彩评论

暂无评论...
验证码 换一张
取 消