0

i have issue about when i search data all checkbox will checked. i want is when i search data . Then data showed must be checked.

example i search the word "vin". only the word have vin must checked/true

this is my html

<input />
<table >
    <tr>
        <th>Name</th>
        <th>Age</th>
    </tr>
</table>
<table class="AvailableGroupLab availGrpLabs avalLabs">
    <tr>
        <td><input type='checkbox'/></td>
        <td><span>wewe</span>

        </td>
        <td>16</td>
    </tr>
    <tr>
        <td><input type='checkbox' /></td>
        <td><span>Melvin</span>

        </td>
        <td>18</td>
    </tr>
    <tr>
        <td><input type='checkbox' /></td>
        <td><span>Marvin</span>

        </td>
        <td>20</td>
    </tr>
</table>

this is my script

function filter(element) {
    var $trs = $('.AvailableGroupLab tr').hide();
    var regexp = new RegExp($(element).val(), 'i');

    var $valid = $trs.filter(function () {
        return regexp.test($(this).children(':nth-child(2)').text())
    }).show();
    $valid.find(':input:checkbox').attr("checked",true);

    $trs.not($valid).hide()
}

$('input:text').on('keyup change', function () {
    filter(this);
})
4

2 回答 2

1

应该清除其他input:checkbox选中的属性。

什么时候$(element).val()“”应该返回false?

像这样 :

$trs.not($valid).hide().find(':input:checkbox').attr("checked",false);
于 2013-09-10T08:08:58.753 回答
0

不确定这是否是你想要的......但你可以尝试:contains

function filter(element) {

var $trs = $('.AvailableGroupLab tr').hide(),
    $value = $(element).val();
  var $searchedElement = $("span:contains(" + $value + ")");
  $searchedElement.parents('tr')
                  .find('input:checkbox').prop('checked', true)
                  .end()
                  .show();
}

在这里摆弄

于 2013-09-10T07:38:38.377 回答