无法在jQuery中调用函数

时间:2015-05-18 05:43:57

标签: javascript jquery

我试图在这个javascript代码中调用一个函数。我的代码需要检查用户是选择var numvar lettersvar symbolstrue还是false。在代码中,我预设了值,但我仍然在对象choices中搜索truepush变量到数组choices_made中的变量。但是,由于我需要随机选择num, letters and symbols出现的顺序,我会根据Math.random()随机选择该类。但是,它之后没有显示alert(jumbled_result)

http://jsfiddle.net/bdaxtv2g/1/

HTML

<input id="num" type="text" placeholder="Enter desired length">
<br/><br/>
<input id="press" type="button" value="jumble it up">

JS

$(document).ready(function(){
var fns={};

$('#press').click(function(){
    var length = parseInt($('#num').val());
    var num = true;
    var letters = true;
    var symbols = false;

    gen(length, num, letters, symbols);
});

function gen(len, num, letters, sym){
    var choices = {
            1:num,
            2:letters,
            3:sym
        };
    var choice_made = ['0'];
    var choice = 0;
    var jumbled_result = '';

    for(x in choices){
        if(choices[x]==true){
            choice_made.push(x);
        }
    }

    for(i=0;i<len;i++){
        var funName = 'choice';
        choice = Math.round(Math.random() * (choice_made.length-1));

        funName += choice_made[choice];
        jumbled_result = fns[funName](jumbled_result);
    }

    alert(jumbled_result);
}

fns.choice0 = function choice0(jumbled_result){
    var numbers = '0123456789';
    return jumbled_result += numbers.charAt(Math.round(Math.random() * numbers.length));
}

fns.choice1 = function choice1(jumbled_result) {
    var alpha = 'abcdefghijklmnopqrstuvwxyz';
    return jumbled_result += alpha.charAt(Math.round(Math.random() * alpha.length));
}

});

2 个答案:

答案 0 :(得分:2)

你永远不会在jQuery的document.ready中声明函数。应在第一次运行期间声明这些函数(除非在特殊情况下)。

这是一段由您的代码制作的代码。我所做的就是从document.ready事件中删除你的功能。

&#13;
&#13;
$(document).ready(function() {
  $('#press').click(function() {
    var length = parseInt($('#num').val());
    var num = true;
    var letters = true;
    var symbols = false;

    gen(length, num, letters, symbols);
  });
});

var fns = {};

function gen(len, num, letters, sym) {
  var choices = {
    1: num,
    2: letters,
    3: sym
  };
  var choice_made = ['0'];
  var choice = 0;
  var jumbled_result = '';

  for (x in choices) {
    if (choices[x] == true) {
      choice_made.push(x);
    }
  }

  for (i = 0; i < len; i++) {
    var funName = 'choice';
    choice = Math.round(Math.random() * (choice_made.length - 1));

    funName += choice_made[choice];
    jumbled_result = fns[funName](jumbled_result);
  }

  alert(jumbled_result);
}
fns.choice0 = function choice0(jumbled_result) {
  var numbers = '0123456789';
  return jumbled_result += numbers.charAt(Math.round(Math.random() * numbers.length));
}

fns.choice1 = function choice1(jumbled_result) {
  var alpha = 'abcdefghijklmnopqrstuvwxyz';
  return jumbled_result += alpha.charAt(Math.round(Math.random() * alpha.length));
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="num" type="text" placeholder="Enter desired length">
<br/>
<br/>
<input id="press" type="button" value="jumble it up">
&#13;
&#13;
&#13;

答案 1 :(得分:1)

它是因为对象选择已被初始化的方式。试试这个......

       var choices = {
            0:num,
            1:letters,
            2:sym
        };

 var choice_made = [];

JS小提琴链接:http://jsfiddle.net/8dw7nvr7/2/