简体   繁体   中英

Jquery find each element by attribute value

I'm trying to get all labels with an attribute of "selected" without a null or empty value. I can't seem to find any labels. Here is the html.

<form id="frmPickGames">
    <div style="margin-top: 150px">
    <div data-bind="foreach: games, visible: games().length > 0">
        <div class="row clearRight borderBottom">
            <div class="col-sm-12 col-md-12 col-lg-12">
                <label class="" data-bind="text: gameDate"></label> / Time:
                <label class="" data-bind="text: gameTimeET"></label> / Tv Station:
                <label class="" data-bind="text: tvStation"></label>
            </div>
            <div id="mode-group" class="btn-group btn-group-lg btn-group-justified" data-toggle="buttons">
                <div class="col-sm-12 col-md-4 col-lg-4 text-center awayColumn">
                    <label class="btn btn-default" data-bind="click: $parent.save, attr: { id: gameId() + '~' + awayTeam(), selected: teamId() }">
                        <input type="radio" data-bind="attr: { name: gameId(), id: gameId() + '~' + awayTeam() }" selected >
                        <img data-bind="attr: { src: awayTeamLogoUrl, alt: awayTeamFullName }" style="height: 100px; width: 150px;" /><br />
                        <label data-bind="text: awayTeamFullName"></label>
                    </label>
                </div>
                <div class="col-sm-12 col-md-2 col-lg-2 text-center" style="padding-top: 50px;">AT</div>
                <div class="col-sm-12 col-md-4 col-lg-4 text-center homeColumn">
                    <label class="btn btn-default" data-bind="click: $parent.save, attr: { id: gameId() + '~' + homeTeam() }">
                        <input type="radio" data-bind="attr: { name: gameId(), id: gameId() + '~' + homeTeam() } ">
                        <img data-bind="attr: { src: homeTeamLogoUrl, alt: homeTeamFullName }" style="height: 100px; width: 150px;" /><br />
                        <label data-bind="text: homeTeamFullName"></label>
                    </label>
                </div>
            </div>
        </div>
    </div>
    </div>
</form>

Here is the javascript/jquery that I'm trying to use to get all the labels so that I can set a class to show that the label was selected. I'm assuming this doesn't work because the labels are too deep in the html code.

$('label[selected!=""]').each(function () {
    alert('hi');

});

If you wanna find the label that contains 'selected' in the 'data-bind'attribute, just use this selector:

$("label[data-bind*='selected']").each(function () {
    alert('hi');
});

It sounds like

$('label').each(function () {
    if($(this).find('input[selected]').size()){
        alert('hi');
    }
});

to find all the labels that have an input that is 'selected'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM