0

好吧,我有一个带有一些 TR 的 html 表,如下所示

<tr id="myId" class="aRandomClass" selected="TRUE">...</tr>
<tr id="myId2" class="aRandomClass" selected="FALSE">...</tr>

并且有一个简单的选择器

var a=$("#"+tableId).find("[selected='TRUE']");
or
var b=$("#"+tableId + " tr[selected='TRUE']");

Firefox 中返回带有 selected='TRUE' 的 TRs,但在 MSIE 8-9 中它什么也不返回。我猜问题是TRs不应该使用“selected”属性,并且IE发现它无效或类似的东西..但是下面的代码返回了所选属性的值..

var c=$("#" + tableId + " tr").first().attr('selected');

还有一点值得注意的是,这段代码也适用于 IE 中的 JQuery 1.4.2。通过升级到 JQuery 1.8.3,它只适用于 FF。

由于某些原因,我想避免更改属性的名称。

我想到的解决方案是一个循环“对于每个 tr,如果 selected='true' add tr in var a”。有更好的建议吗?

4

2 回答 2

1

First you have duplicate IDs which should be unique. Secondly you can select rows with tr[selected="true"]'

And instead of that attribute you should use data-selected so you have a valid markup.

于 2013-06-07T10:24:21.357 回答
1

我会建议使用 .prop() 代替 jq 1.8.3,但真的不确定它可以在这里帮助你:

var $trsSelected = $("#"+tableId).find('tr').filter(function(){return $(this).prop('selected')=='TRUE'});

var $trsNotSelected = $("#"+tableId).find('tr').filter(function(){return $(this).prop('selected')!='TRUE'});
于 2013-06-07T10:44:00.210 回答