我在編碼方面稍稍休息了幾個月,現在我正試圖再次回到它。如何檢查字符串是否包含數組值?
我正在開發Facebook遊戲的用戶手冊,它將收集與遊戲有關的所有帖子的數據,並使用該數據開始接受程序。
但是,我想介紹一個選項,讓用戶選擇他們更願意接受哪種獎勵類型,並且即時應用它有困難。
我想要做的是取文章標題字符串,並將其與選定的選項(複選框值)數組進行比較,以查看帖子標題是否包含數組中的任何值。
這是選項面板,用戶可以選擇接受哪些職位:
$("#rightCol").prepend('<div id="bonus_options_panel">'
+'<ul id="bonuses"><legend><b>Select Bonuses to accept...</b></legend>'
+'<li><label>Hungry No More <input type="checkbox" value="heroic companions!" class="bonus_select_option"></label></li>'
+'<li><label>Pikemen <input type="checkbox" value="extra pikes!" class="bonus_select_option"></label></li>'
+'<li><label>Swordsmen <input type="checkbox" value="recruited swordsmen!" class="bonus_select_option"></label></li>'
+'<li><label>Valuable Treasure <input type="checkbox" value="valuable treasure!" class="bonus_select_option"></label></li>'
+'<li><label>Jousting Competition <input type="checkbox" value="jousting game!" class="bonus_select_option"></label></li>'
+'<li><label>Gamble <input type="checkbox" value="wants to gamble." class="bonus_select_option"></label></li>'
+'<li><label>Extra Lumber <input type="checkbox" value="has extra lumber!" class="bonus_select_option"></label></li>'
+'<li><label>Foreign Traders <input type="checkbox" value="attracted foreign traders!" class="bonus_select_option"></label></li>'
+'<li><label>Buxom Wenches <input type="checkbox" value="inviting friends over!" class="bonus_select_option"></label></li>'
+'<li><label>Plague <input type="checkbox" value="kingdom has the plague!" class="bonus_select_option"></label></li>'
+'<li><label>Legendary Bard <input type="checkbox" value="hosting a legendary bard!" class="bonus_select_option"></label></li>'
+'<li><label>Wild Horses <input type="checkbox" value="found wild horses!" class="bonus_select_option"></label></li>'
+'<li><label>Mercenary Armies <input type="checkbox" value="mercenary armies with you." class="bonus_select_option"></label></li>'
+'<li><label>Famed Hero <input type="checkbox" value="feasting with a famed hero!" class="bonus_select_option"></label></li>'
+'<li><label>Queen Parading <input type="checkbox" value="Queen is parading!" class="bonus_select_option"></label></li>'
+'<li><label>Wanted Criminal <input type="checkbox" value="caught a wanted criminal!" class="bonus_select_option"></label></li>'
+'<li><label>Rolling Logs <input type="checkbox" value="has extra logs!" class="bonus_select_option"></label></li>'
+'<li><label>Archery Game <input type="checkbox" value="holding a Archery game!" class="bonus_select_option"></label></li>'
+'</ul>'
+'</div>');
,這裏是一個帖標題字符串的例子: 利亞姆·艾倫是需要英雄的同伴!
我一直在創造複選框值的這樣一個數組:
var bonus = [];
$("input.bonus_select_option:checked").each(function(){
bonus.push($(this).val());
});
什麼,我想現在要做的就是,把帖子標題字符串,並檢查它是否包含任何的數組的值,但我很難做到這一點。
任何幫助將不勝感激,並提前致謝!
你需要看看array.indexOf - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf – Archer
爲我工作http:// jsfiddle。 net/Sx9pU/ – Jai
@Archer我曾嘗試使用IndexOf,但我的數組值只包含部分字符串,它們不完全相同 –