我有一张表格,表格中只有正常<input type="text">
表格。但我想要做的是使用randompassword生成器为密码字段生成密码。该按钮需要激活脚本并将值放入密码字段。
但是不能以表格的形式提交表格吗?
示例代码:
<?php
//password gen
if (isset($_POST['passgen'])) {
$charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$generated_password = substr(str_shuffle($charset), 0, 12);
}
//password gen end
?>
<form action="index.php" method="POST">
<input type="submit" name="submit-form-with-text" value="submit form"/>
<input type="submit" name="activate-the-passgen" value="Generate a password"/>
<input type="text" value="<?php echo $generated_password; } ?>" size="15" />
</form>
答案 0 :(得分:1)
如果你想使用jquery,那就试试吧: 演示:http://jsfiddle.net/qsDn5/7/
<强> JS 强>
$(document).ready(function(){
$("#genPass").click(function(){
chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
var result = '';
for (var i = 15; i > 0; --i)
result += chars[Math.round(Math.random() * (chars.length - 1))];
$("#pass").val(result);
})
});
<强> HTML 强>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<form action="index.php" method="POST">
<input type="submit" name="submit-form-with-text" value="submit form"/>
<input type="button" id="genPass" name="activate-the-passgen" value="Generate a password"/>
<input type="text" value="" id="pass" size="15" />
</form>