javascript 如何通过点击按钮为文本框赋值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19082447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to assign a value to a textbox by onclick button?
提问by Rajesh Kalluri
I am generating passwords automatically by using php. I stored the generated password in a variable. Here i have assign those generated passwords to a text box by using ONCLICK property on button(button name is "Generate Password"). But i am not getting required output(password must be visible in textbox by clicking that button). Here i have attached respective code. Can anyone tell what i had done wrong??
我正在使用 php 自动生成密码。我将生成的密码存储在一个变量中。在这里,我通过使用按钮上的 ONCLICK 属性将这些生成的密码分配给文本框(按钮名称为“生成密码”)。但我没有得到所需的输出(通过单击该按钮,密码必须在文本框中可见)。我在这里附上了相应的代码。谁能告诉我我做错了什么??
<!DOCTYPE html>
<html>
<head>
<title>create_profile</title>
<link rel="stylesheet" type="text/css" href="create_profile.css" />
<script type="text/javascript">
function getPassword(){
document.getElementById("password").value="<?php
// Characters to use for the password
$str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
// Desired length of the password
$pwlen = 8;
// Length of the string to take characters from
$len = strlen($str);
// RANDOM.ORG - We are pulling our list of random numbers as a
// single request, instead of iterating over each character individually
$uri = "http://www.random.org/integers/?";
$random = file_get_contents(
$uri ."num=$pwlen&min=0&max=".($len-1)."&col=1&base=10&format=plain&rnd=new"
);
$indexes = explode("\n", $random);
array_pop($indexes);
// We now have an array of random indexes which we will use to build our password
$pw = '';
foreach ($indexes as $int){
$pw .= substr($str, $int, 1);
}
echo $pw;
回声 $pw;
?>"; }
?>"; }
</script>
<h1> Create Profile </h1>
<label for="name">name:</label><br />
<input id="name" name="name" type="text" size="30" /><br />
<label for="user_id">user id:</label><br />
<input id="user_id" name="user_id" type="text" size="30" /><br />
<label for="email">Your Email:</label><br />
<input id="email" name="email" type="text" size="60" /><br />
<label for="password">password:</label><br />
<input id="password" name="password" size = "10" /> <br/>
<button onclick="getpassword();" >Generate Password</button>
回答by iCode
The correction of your suggestion
您的建议的更正
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>ab</title>
<script type="text/javascript">
<?php $pw="hello"; ?>
// You've written here getPassword()
function getpassword(){
document.getElementById("password").value="<?php echo $pw; ?>";
}
</script>
</head>
<label for="password"> password: </label><br />
<input id="password" name="password" size = "10" />
<!-- there is no need for a semicolon here -->
<button onclick="getpassword()" >Generate Password</button>
<body>
</body>
</html>
without php:
没有 php:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>ab</title>
<script type="text/javascript">
// You've written here getPassword()
function getpassword(){
document.getElementById("password").value="hello";
}
</script>
</head>
<label for="password"> password: </label><br />
<input id="password" name="password" size = "10" />
<!-- there is no need for a semicolon here -->
<button onclick="getpassword()" >Generate Password</button>
<body>
</body>
</html>
回答by Orange_shadow
first-time learn javascript and jquery(it is javascript library) :) if you use jquery
第一次学习javascript和jquery(它是javascript库):)如果你使用jquery
<button onclick="function(){ $.get('/url/',function(dataReturnFromServer){alert(dataReturnFromServer);})}"></button>