如何将带有“onclick”的javascript输出到文本字段中?

时间:2017-07-05 09:27:24

标签: javascript html function input

我写了一个javascript程序来生成随机密码。如何将结果输出到文本字段(输入标记)?

var randomString = function(length) {
  var text = "";
  var possible = "!@[|^]µ§$%&?*€#ABCDEFGHIJKLMNOPQRSTVWXYZabcdefghijklmnopqrstvwxyz1234567890";
  for (var i = 0; i < length; i++) {
    text += possible.charAt(Math.floor(Math.random() * possible.length));
  }
  return text;
}

var rs = randomString(16);
console.log(rs);
<section id="passgen">
  <button type="button" id="pass-button" onclick="**???**">Generate Password</button>
  <br><br>
  <input type="text" name="output">
</section>

5 个答案:

答案 0 :(得分:4)

添加内联点击事件不是一个好主意,您可以使用addEventListener代替。

所以在这里,我首先使用document.getElementById选择元素并为其添加click事件,然后使用document.getElementsByName()我选择输出元素并打印随机字符串

另请注意与[0]一起使用的document.getElementsByName()是因为如果代码遇到名称为output的多个元素,我们会选择第一个元素,因为它会返回一个数组匹配的元素。如果您可以向id字段添加一些input,然后使用document.getElementById()来选择特定元素,那就太棒了。

var randomString = function(length) {
  var text = "";
  var possible = "!@[|^]µ§$%&?*€#ABCDEFGHIJKLMNOPQRSTVWXYZabcdefghijklmnopqrstvwxyz1234567890";
  for (var i = 0; i < length; i++) {
    text += possible.charAt(Math.floor(Math.random() * possible.length));
  }
  return text;
}

document.getElementById('pass-button').addEventListener('click', function() {
  document.getElementsByName('output')[0].value = randomString(16);
});
<section id="passgen">
  <button type="button" id="pass-button">Generate Password</button>
  <br>
  <br>
  <input type="text" name="output">
</section>

答案 1 :(得分:1)

你可以这样做:

var randomString = function(length) {
        var text = "";
        var possible = "!@[|^]µ§$%&?*€#ABCDEFGHIJKLMNOPQRSTVWXYZabcdefghijklmnopqrstvwxyz1234567890";
        for(var i = 0; i < length; i++) {
            text += possible.charAt(Math.floor(Math.random() * possible.length));   
        }
        return text;
    }

    var rs = randomString(16);

    console.log(rs);

    document.getElementsByName('output')[0].value = rs;

答案 2 :(得分:0)

非常简单document.getElementById("id_of_input_field").value = rs; 或者你可以使用输入框的名称来定位document.getElementsByName('output')[0].value = rs;

答案 3 :(得分:0)

primaryColor
var randomString = function(length) {
        var text = "";
        var possible = "!@[|^]µ§$%&?*€#ABCDEFGHIJKLMNOPQRSTVWXYZabcdefghijklmnopqrstvwxyz1234567890";
        for(var i = 0; i < length; i++) {
            text += possible.charAt(Math.floor(Math.random() * possible.length));   
        }
        return text;
    }

var generatePassword = function(length) {
document.getElementById('password').value = randomString(length);
}

答案 4 :(得分:0)

为您的输入提供ID,然后:

import numpy as np

a = 58682.7578125
f32 = np.float32(a)
f64 = np.float64(a)

u32 = np.array(a, dtype=np.float32).view(dtype=np.uint32)
u64 = np.array(a, dtype=np.float64).view(dtype=np.uint64)

b32 = bin(u32)[2:]
b32 = '0' * (32-len(b32)) + b32  # add leading 0s
print('32 bit: ', b32)
print('sign    : ', b32[0])
print('exponent: ', b32[1:9])
print('fraction: ', b32[9:])
print()

b64 = bin(u64)[2:]
b64 = '0' * (64-len(b64)) + b64  # add leading 0s
print('64 bit: ', b64)
print('sign    : ', b64[0])
print('exponent: ', b64[1:12])
print('fraction: ', b64[12:])