2014-01-30 170 views
0

我在编码方面稍稍休息了几个月,现在我正试图再次回到它。如何检查字符串是否包含数组值?

我正在开发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()); 
}); 

什么,我想现在要做的就是,把帖子标题字符串,并检查它是否包含任何的数组的值,但我很难做到这一点。

任何帮助将不胜感激,并提前致谢!

+0

你需要看看array.indexOf - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf – Archer

+0

为我工作http:// jsfiddle。 net/Sx9pU/ – Jai

+0

@Archer我曾尝试使用IndexOf,但我的数组值只包含部分字符串,它们不完全相同 –

回答

1

尝试Array.join和String.search方法:

var title = 'Liam Allan is need of heroic companions!'; 
    var bonus = []; 
    $("input.bonus_select_option:checked").each(function(){ 
     bonus.push($(this).val()); 
    }); 
    $.each(bonus, function(key, value){ 
     if(title.indexOf(value) > 0) ... //Do something 
    }); 

例如:

var re = new RegExp('(' + bonus.join('|') + ')'); 
console.log('Liam Allan is need of heroic companions!'.search(re) != -1); // true if contains 
0

与您如何填写您的奖金变量开始

var title = 'Liam Allan is need of heroic companions!'; 
    var bonus = []; 
    bonus.push('heroic companions!'); 
    bonus.push('recruited swordsmen!'); 
    bonus.push('feasting with a famed hero!'); 

    $.each(bonus, function(key, value){ 
    if(title.indexOf(value) > 0) alert(value + " found in title"); 
    }); 

会返回

heroic companions! found in title 
相关问题