如何返回输出函数javascript内部html?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (160)

我想输出一个function secret();所以看起来是这样的:

AAAA-BBB-<random string>

但是它返回这个错误: secret is not a function

以下是代码:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <title>Random End String</title>
  <script type="text/javascript">
    function writeName() {
      var welcomeMsg = document.getElementById('welcome');
      var name = document.getElementById('name');
      var alamat = document.getElementById('alamat');
      var secret = secret();
      var formContent = document.getElementById('entername');
      welcomeMsg.innerHTML = "Your Identifier is " + name.value + "-" + alamat.value + "-" + secret + "";
      formContent.innerHTML = "";
    }
  </script>
  <script>
    function secret() {
      var text = "";
      var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
      for (var i = 0; i < 5; i++)
        text += possible.charAt(Math.floor(Math.random() * possible.length));
    }
  </script>
</head>
<body>
  <p id="welcome"></p>
  <form id="entername">
    Your Name:<input type="text" id="name" /><br/> LockerID:
    <input type="text" id="alamat" /><br/>
    <input type="hidden" id="secret">
    <input type="button" value="Submit" onclick="writeName();" />
  </form>
</body>
</html>

提问于
用户回答回答于

完整的示例:

function writeName() {
  var welcomeMsg = document.getElementById('welcome');
  var name = document.getElementById('name');
  var alamat = document.getElementById('alamat');
  var secret1 = secret();
  var formContent = document.getElementById('entername');
  welcomeMsg.innerHTML = "Your Identifier is " + name.value + "-" + alamat.value + "-" + secret1 + "";
  formContent.innerHTML = "";
}

function secret() {
  var text = "";
  var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

  for (var i = 0; i < 5; i++) {
    text += possible.charAt(Math.floor(Math.random() * possible.length));
  }
  return text
}

function writeName1() {
  var welcomeMsg = document.getElementById('welcome');
  var name = document.getElementById('name');
  var alamat = document.getElementById('alamat');
  var text = "";
  var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

  for (var i = 0; i < 5; i++) {
    text += possible.charAt(Math.floor(Math.random() * possible.length));
  }
  var formContent = document.getElementById('entername');
  welcomeMsg.innerHTML = "Your Identifier is " + name.value + "-" + alamat.value + "-" + text + "";
  formContent.innerHTML = "";
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8" />
  <title>Random End String</title>
</head>

<body>
  <p id="welcome"></p>
  <form id="entername">
    Your Name:<input type="text" id="name" /><br/> LockerID:
    <input type="text" id="alamat" /><br/>
    <input type="hidden" id="secret">
    <input type="button" value="Submit" onclick="writeName();" />
  </form>
</body>

</html>

Your Name:<input type="text" id="name1" /><br/> LockerID:
<input type="text" id="alama1t" /><br/>
<input type="hidden" id="secret1">
<input type="button" value="Submit" onclick="writeName1();" />

用户回答回答于

你的局部变量secret隐藏你的功能secret。只需将变量重命名为其他方法就可以解决此问题。

function writeName(){
 var welcomeMsg = document.getElementById('welcome');
 var name = document.getElementById('name');
 var alamat = document.getElementById('alamat');
 var secret = secret(); //right here this is your issue
 var formContent = document.getElementById('entername');

 welcomeMsg.innerHTML = "Your Identifier is "+name.value+"-"+alamat.value+"-"+secret+""; 
 formContent.innerHTML = "";

 }

扫码关注云+社区

领取腾讯云代金券