开发者

Trouble selecting checkboxes inside each() cycle

开发者 https://www.devze.com 2023-02-20 14:26 出处:网络
I am trying to iterate over each text input field and find out whether a checkbox assigned to it is checked or not. I am having trouble selecting checkboxes inside the each() cycle though.

I am trying to iterate over each text input field and find out whether a checkbox assigned to it is checked or not. I am having trouble selecting checkboxes inside the each() cycle though.

My HTML:

<table>
        <tbody>
                <tr>
                    <td style="vertical-align: top;">
                        <label for="MainTemplate" class="">Hlavná šablóna:</label>
                    </td>
                    <td>
                        <input name="MainTemplate" class="input mainTemplate" value="" style="width: 10em;" type="text"> <br> <table>

    <tbody><tr>         <td>            <input name="MainTemplateInheritCheckbox" class="input inheritCheckbox" value="1" checked="checked" type="checkbox">        </td>       <td style="font-size: 0.9em;">          <label for="MainTemplateInheritCheckbox">hodnoda sa zdedí z vyššej úrovne</label>       </td>   </tr>

</tbody></table>
                        <br>
                    </td>
                </tr>
                <tr>
                    <td style="vertical-align: top;">
                        <label for="Lector" class="">Školiteľ:</label>
                    </td>
                    <td>

                        <input name="Lector" class="input lector" value="" style="width: 10em;" type="text"> <br> <table> 开发者_JAVA技巧  <tbody><tr>         <td>            <input name="LectorInheritCheckbox" class="input inheritCheckbox" value="1" checked="checked" type="checkbox">      </td>       <td style="font-size: 0.9em;">          <label for="LectorInheritCheckbox">hodnoda sa zdedí z vyššej úrovne</label>

        </td>   </tr> </tbody></table>
                        <br>
                    </td>
                </tr>
</table>

My jquery code:

        $("input[type=text]").each(function() {
            // here I need to find out whether the checkbox is checked or not
        });

I tried this but it returns false even for checked checkboxes:

alert($(this).next().next().children("input[type=checkbox]:first").is(":checked"));


Try $(this).next('table').find('input[type=checkbox]:first')is(':checked');

You could also probably get it by name since they're related.

var cbName = '#' + $(this).attr('name') + 'InheritCheckbox';
$(cbName).is(':checked');

Both of these are a little bit fragile and depend on relationships either in the DOM or between the element names, though I suspect the latter might be more stable over time.


Try this(get the containing table row and search for the checkbox within that element):

$("input[type=text]").each(function() {
    alert($(this).closest("tr").find("input[type=checkbox]:first").is(":checked"));
}); 


Try

$("input[type=checkbox]:first",$(this).parent()).is(":checked")

jsFiddle


$("input[type=text]").each(function()
{
    alert($(this).closest("td").find("input[type=checkbox]").attr("checked"));
}); 
0

精彩评论

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