简体   繁体   English

jQuery中的字符串比较find()无法与

[英]string comparison find() in jquery isnt working with <

I am playing with xml on client side. 我在客户端玩xml。 below is my XML. 下面是我的XML。

<ItemList>
    <Row ID="" Name="-- Select a Item --" GrpID="" Flag=""/>
    <Row ID="S5"  Name="Item 1" GrpID="G7" Flag="0"/>
    <Row ID="S6"  Name="Item 2" GrpID="G7" Flag="0"/>
    <Row ID="S7"  Name="Item 3" GrpID="G7" Flag="0"/>
    <Row ID="S85" Name="Item 4" GrpID="G7" Flag="0"/>
    <Row ID="S5"  Name="Item 11" GrpID="G4" Flag="0"/>
    <Row ID="S13" Name="Item 5" GrpID="G7" Flag="0"/>
    <Row ID="S14" Name="Item 6" GrpID="G7" Flag="0"/>
    <Row ID="S15" Name="Item 7" GrpID="G7" Flag="0"/>
    <Row ID="S16" Name="Item 8" GrpID="G7" Flag="0"/>
    <Row ID="S17" Name="Item 9" GrpID="G7" Flag="0"/>
    <Row ID="S12" Name="Item 12" GrpID="G4" Flag="0"/>
    <Row ID="S22" Name="Item 22" GrpID="" Flag="0"/>
    <Row ID="S25" Name="Item 26" GrpID="" Flag="0"/>
</ItemList>

I have to assign items with (GrpID=G7 and ID<'S5') OR (GrpID is '') 我必须分配具有(GrpID = G7并且ID <'S5')或(GrpID为'')的项目

$(xmlItems).find("Row[GrpID='7'][ID<'S82'],[GrpID='']")

Where am I doing wrong. 我在哪里做错了。 I could get with the below. 我可以得到以下。

$(xmlItems).find("Row[GrpID='7'][ID='S85'],[GrpID='']")

the problem "=" is working and "<" is not working for Item ID (strings) comparison. 问题“ =”有效,而“ <”不适用于商品ID(字符串)比较。

Jquery uses CSS selectors; jQuery使用CSS选择器; the syntax does not support matching with < or > . 语法不支持与<>匹配。 You need to do this using the jQuery.filter() , for example: 您需要使用jQuery.filter()进行此操作,例如:

$filtered = $(xmlItems).find("Row[GrpID='G7']").filter(
    function (index, element) {
        return element.ID < 'S5';
    }
);

However notice that the string comparison still might not do what you mean - this is not a numeric comparison and 'S11' < 'S2' is true. 但是请注意,字符串比较仍然可能不符合您的意思-这不是数字比较,并且'S11' < 'S2'为true。

The return statement in @AnttiHaapala should be adjusted slightly for the code to work. @AnttiHaapala中的return语句应稍作调整,以使代码正常工作。 Please see the modified version below: 请在下面查看修改后的版本:

$filtered = $(xmlItems).find("Row[GrpID='G7']").filter(
    function (index, element) {
        return $(element).attr('ID') < 'S5';
    }
)
.add( $(xmlItems).find( 'row[GrpID=""]' ) );

This is because ID is an attribute of row . 这是因为IDrow的属性。 Since element is an iterator for row , you want to grab it's ID attribute like this: 由于element是row的迭代器,因此您想像这样获取其ID属性:

$(element).attr( 'ID' );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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