2016-08-23 35 views
0

如何比較此列表中的字符串以驗證此列表中存在「暫停」,但它不是「校園暫停」的一部分?如果「暫停」這個詞的索引可以工作,我該如何實現?jquery分析ul列表中的字符串值

<ul id='act_ul_0'> 
    <li id="act_l1_0"> 
    <div class="itemlabel"> 
     Suspension 
    </div> 
    </li> 
</ul> 
<div class="itemlabel"> 
    <ul id='act_ul_1'> 
    <li id="act_l1_0"> 
     On-Campus Suspension 
    </li> 
    </ul> 
</div> 
+1

參見JavaScript的['的indexOf()'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf)。你也可能會發現jQuery的[':contains()'selector](http://api.jquery.com/contains-selector/)很有用。 – showdev

+0

@showdev謝謝!這真的很有幫助。我在列表中循環,如果indexof value> 0,它在校園內,當0:暫停時。 – Kaur

回答

1
// Load the list items into an jQuery object/array 
var $items = $('#act_ul_0').find('li'); 
// Define the string that you intend to match 
var searchItem = "Suspension"; 
// Use the [jQuery.inArray()][1] function to search for your match 
if ($.inArray(searchItem, $items) > -1) { 
    // Pretty self-explanatory from here on, right? 
    console.log("I found it!"); 
} else { 
    console.log("No bueno!"); 
} 
+0

SO警察..幫助! – DevlshOne

+1

那個警察殘酷的壽! = P --- PS:問題不再。過時的噪音消除了。已添加+1。 – XenoRo