不需要正则表达式。您可以使用索引和简单的 HTML 属性来做到这一点!
首先,HMTL
<div class="instructions">
Please select <span class="choices"></span>
</div>
<button id="cb-8" type="button" class="btn captcha" data-caption="The Eye">
<i class="icon-eye-open"></i>
</button>
<button id="cb-5" type="button" class="btn captcha" data-caption="The Home">
<i class="icon-home"></i>
</button>
<button id="cb-2" type="button" class="btn captcha" data-caption="The File">
<i class="icon-file"></i>
</button>
<button id="cb-1" type="button" class="btn captcha" data-caption="The Flag">
<i class="icon-flag"></i>
</button>
<button id="cb-3" type="button" class="btn captcha" data-caption="The Tag">
<i class="icon-tag"></i>
</button>
<button id="cb-4" type="button" class="btn captcha" data-caption="The Calendar">
<i class="icon-calendar"></i>
</button>
<br/><br/>
<button type="button" class="btn check-answer">
<i id="icon-check" class="icon-check"></i>Check Answer
</button>
还有剧本
$(function() {
// Number of buttons we want to show
var num_show_buttons = 4;
// Numbers of buttons that will be the answers.
var num_good_buttons = 2;
// Hide all buttons by default.. and cache it.
var all_btn = $(".captcha").hide();
// Get random numbers to know which buttons we're going to show
var rand = getRandomNumbers({nums: num_show_buttons, max: all_btn.length})
// Get random numbres to know which ones are going to be the answers
var good_rand = getRandomNumbers({nums: num_good_buttons, max: 3})
// Crawl through each buttons and display the chosen ones.
$.each(rand, function(x,y) {
var t_btn = all_btn.eq(y).addClass("visible").show();
})
// Crawl through each good buttons, get the captions, and add a class so we know its the good ones.
$.each(good_rand, function(x,y) {
var t_btn = $(".captcha.visible").eq(y).addClass("good");
// This outputs the icons to select to the user.
$(".choices").append(t_btn.data('caption') + ", ");
})
// Everytime a buttons is clicked, add the active class. If already active, remove it.
$(document).on("click", '.captcha', function() {
var btn = $(this);
if (btn.is(".active")) {
btn.removeClass("active");
} else {
btn.addClass("active")
}
});
$(document).on("click", ".check-answer", function() {
// By default, the validation is false
var validation = false;
// If the number of active buttons equals the numbers of good buttons...
if ($(".captcha.active").length == num_good_buttons) {
// Set it the validation to true! But it's not over yet..
var validation = true;
// Let's crawl through each active one...
$(".captcha.active").each(function(x,y) {
// ... and check if they're good! If not, validation is false!
if ($(".captcha.active")[x] != $(".captcha.good")[x]) {
validation = false;
}
})
}
// Display results
if (validation == true) {
alert ("Good stuff!");
} else {
alert ("Uh oh :(");
}
})
});
// Functions to get x random numbers between range
function getRandomNumbers(params) {
def = {
nums: 10,
min: 0,
max: 100
}
opt = $.extend({}, def, params);
var arr = []
while(arr.length < opt.nums) {
var randomnumber = Math.floor( Math.random() * opt.max) + opt.min
var found=false;
for(var i=0;i<arr.length;i++) {
if (arr[i] == randomnumber) {
found = true;
break;
}
}
if (!found) {
arr[arr.length] = randomnumber;
}
}
return arr;
}
这是一个有效的 jsfiddle:http: //jsfiddle.net/rx7Bx/1/