我从编码中休息了几个月,现在我想再次回到它。
我正在制作一个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 :(得分:1)
尝试使用Array.join和String.search方法:
var re = new RegExp('(' + bonus.join('|') + ')');
console.log('Liam Allan is need of heroic companions!'.search(re) != -1); // true if contains
答案 1 :(得分:0)
从您填充奖励变量的方式开始:
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 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