单击按钮后在输入字段中生成密码

时间:2019-05-03 14:31:48

标签: javascript jquery html input

此代码不起作用,我不知道为什么要学习JavaScripta,但是我第一次这样做是在寻找互联网解决方案的同时,我发现了类似的代码生成方式,并且可以正常工作

HTML,JavaScript,JQuery

function gencode(length) {
    var result           = "";
    var characters       = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    var letters          = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    var numbers          = "0123456789"
    var charactersLength = characters.length;
    for ( var i = 0; i < length; i++ ) {
        result += characters.charAt(Math.floor(Math.random() * charactersLength));
    }
    return result;
};

$('#btn-generate').click(function(){
    $('#secret-code').val = gencode(8);
});


<div class="form-row">
                                    <div class="form-holder">
                                        <fieldset>
                                            <legend>'.$lang['register']['form']['secret-code']['header'].'</legend>
                                            <input type="text" id="secret-code" name="secret-code" placeholder="'.$lang['register']['form']['example']['secret-code'].'" class="required">
                                        </fieldset>
                                    </div>
                                    <div class="form-holder">
                                        <div class="center">
                                            <a class="btn tooltip tt50 generate" id="btn-generate">
                                                <span class="gencode">
                                                    <em>'.$lang['register']['form']['secret-code']['button'].'</em><i id="icon-gencode" class="fa fa-code fa-2x"></i>
                                                </span>
                                            </a>
                                        </div>
                                    </div>
                                </div>

3 个答案:

答案 0 :(得分:1)

val()是一种方法,因此要更新值,可以按以下方式执行该方法:

  

设置匹配元素集中每个元素的值。

function gencode(length) {
  var result = "";
  var characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  var numbers = "0123456789"
  var charactersLength = characters.length;
  for (var i = 0; i < length; i++) {
    result += characters.charAt(Math.floor(Math.random() * charactersLength));
  }
  return result;
};

$('#btn-generate').click(function() {
  $('#secret-code').val(gencode(8));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<div class="form-row">
  <div class="form-holder">
    <fieldset>
      <legend>Legend</legend>
      <input type="text" id="secret-code" name="secret-code" placeholder="" class="required">
    </fieldset>
  </div>
  <div class="form-holder">
    <div class="center">
      <a class="btn tooltip tt50 generate" id="btn-generate">
        <span class="gencode">
            Generate
            <i id="icon-gencode" class="fa fa-code fa-2x"></i>
        </span>
      </a>
    </div>
  </div>
</div>

答案 1 :(得分:0)

要在jquery中设置值,您应该使用 val 作为功能

$('#secret-code').val(gencode(8));

答案 2 :(得分:0)

$('#secret-code').val(gencode(8));

它只是设置每个匹配元素的值。您可以从下面的链接中阅读所有说明:

http://api.jquery.com/val/