jQuery执行正则表达式的多个选项

时间:2012-10-13 19:59:51

标签: jquery regex

我正在尝试设置一种基于三个堆叠按钮的验证码,这些按钮显示视觉上不同的图标。该脚本呈现其中三个按钮(如下所示),我需要验证用户是否根据给出的指示单击了正确的图标。

但是要给出指令,我需要知道哪些图标已经渲染,所以我需要jQuery继续这三个按钮,选择i id属性然后从加载的内容中选择两个(随机)。例如,在下面的按钮中,我会给出“点击'文件'和'主页'”的说明,以便提交表格。

我现在最大的挑战是让jQuery知道已呈现的内容并获取每个属性的名称,以便我可以检查按钮是否已被点击,这意味着现在已经设置了“活动”类。我认为这可以使用正则表达式但不确定如何。我尝试了一些不同的方法,但没有成功。请注意,“活动”类是由jQuery addClass在点击时设置的,所以最初该类只是“btn”。

<button id="cb-8" type="button" class="btn active">
    <i id="icon-eye-open" class="icon-eye-open"></i>
</button>
<button id="cb-5" type="button" class="btn active">
    <i id="icon-home" class="icon-home"></i>
</button>
<button id="cb-2" type="button" class="btn active">
    <i id="icon-file" class="icon-file"></i>
</button>

提前感谢您提供任何帮助。

1 个答案:

答案 0 :(得分:4)

不需要正则表达式。您可以使用索引和简单的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/