使用相同表单中的按钮激活脚本

时间:2013-12-30 10:58:56

标签: javascript php html forms

我有一张表格,表格中只有正常<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>

1 个答案:

答案 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>