使用javascript

时间:2016-09-09 00:47:17

标签: javascript html arrays push

我有一个密码生成器工作正常。但需要一点改变。下图显示 enter image description here

点击“生成密码”按钮后,会生成一个密码。 必填:当我再次点击该按钮时,我需要在下面生成另一个密码而不清除之前的密码。在循环中尝试了几个变种但是没有用。

**passGen.js**

function passGen() {
var Generator = {};
Generator.generateMnemonic = function(length, num_length, mixcase) {
var ret = '';
var vowels = 'aeioe';
var consonants = 'bcdfghklmnpqrstvwxzy';
if (mixcase) {
    vowels += vowels.toUpperCase();
    consonants += consonants.toUpperCase();
}
vowels = vowels.split('');
consonants = consonants.split('');

for(var i = 0; i < length / 2; i++) {
    ret += vowels.getRandom();
    ret += consonants.getRandom();
}

if (!num_length) return ret;

var pos = $random(2, length - 2 - num_length);
return ret.substr(0, pos) + $random(Math.pow(10, num_length - 1),    Math.pow(10, num_length) - 1) + ret.substr(pos + num_length);
    };
    var observe = new Observer('#generator-length, #generator-num_length,  #generator-mixcase, #generator-amount', function(values) {
    var length = values[0].toInt();
    var num_length = values[1].toInt();
    var mixcase = values[2].toInt();
    var amount = values[3].toInt();

    // Fill passwords in a loop
    var words = [];
    for (var i = 0; i < amount; i++) {
        words.push(Generator.generateMnemonic(length, num_length, mixcase)   );

    }

    // Get the output area
    var output = $('generator-output');

    // Output it and highlight it so users will notice the update
    output.value = words.join("\n");
    output.getParent().highlight('#ff8', '#fff');


   }, {
   //   periodical: 1000 // interval in ms
   });

   // To fill in the first values
   observe.fire();
   }
   **Part of Hmtl**
 <script type="text/javascript" src="passGen.js"></script> 
<span>How many passwords:</span>
    <br>
    <select name="amount" id="generator-amount">
        <option value="1" selected>1</option>
        <option value="5">5</option>
        <option value="10">10</option>
        <option value="50">50</option>
        <option value="100">100</option>
    </select>
</label>
<input type="button" name="button" value="Generate Password" onclick="passGen();">
<label>
<br>
    <span>Your passwords:</span>


             

1 个答案:

答案 0 :(得分:0)

沿着这些方面做一些事情:(给出感觉的小例子)带有静态变量。

&#13;
&#13;
function passGen() {
 if ( typeof passGen.words == 'undefined' ) { /* It has not been called do initialization*/
 passGen.words = [];}//else previous passwords persist, and you push, onto them.
 passGen.words.push("Hello");
 alert(passGen.words);
}
passGen();
passGen();
&#13;
&#13;
&#13;

在你的情况下保留我的首字母,删除你的行

var words = [];

并预装passGen。你的words.push和words.join

改编自Static variables in JavaScript