在jquery post之后记住密码

时间:2012-12-03 12:01:46

标签: javascript jquery

这是代码

<script type='text/javascript'>

$("#focusform").submit(function(event) 
{

event.preventDefault();

  var $form = $( this ),

        usname = $form.find( 'input[name="name"]' ).val(),

        uspass = $form.find( 'input[name="pass"]' ).val();
        if($('#chk').is(':checked')) var checked='yes';
        else var checked='no';
          $.post("/login/submit/", { name: usname, pass: uspass, checkbox: checked, template_framed:"yes",submit: "yes" }, function(data)
 {


     if(data=='error'){
       alert("You have made an error");

       return false;

     }
     else{
        if(checked=='yes')window.location='/usercookie/';
        else window.location='/login/success/';
        return true;

     }
  });

 });

 </script>

但是浏览器不想提示是否保存密码。你能帮帮我吗?

3 个答案:

答案 0 :(得分:1)

我会做一个预检查并使用Ajax来检查是否正确,这会返回错误或成功消息,如果成功继续发布表单,否则使用Ajax显示错误

答案 1 :(得分:0)

如果您的<form>没有action网址且没有提交按钮,则浏览器不会提供保存密码。您的密码字段也必须是<input type="password" />

当您尝试以这种方式使用jQuery分配submit函数时,它不起作用:

$("#focusform").submit( ...

但是,如果您向表单添加onsubmit属性,它确实有效:

<form id="focusForm" action="page.php" method="post" onsubmit="return mySubmit()">
                                                               ^^ here

然后在提交函数中返回false:

function mySubmit()
{
    // do the jquery ajax or whatever you want to do here

    return false;
}

答案 2 :(得分:0)

<iframe id="temp" name="temp" src='/engine/blank.html' style="display:none"></iframe>
<form id='focusform' target='temp' method='post' onsubmit="return mySubmit()">
...
</form>
<script type='text/javascript'>

function mySubmit()
{

  var $form = $("#focusform"),

        usname = $form.find( 'input[name="name"]' ).val(),

        uspass = $form.find( 'input[name="pass"]' ).val();
        var checked = ($('#chk').is(':checked')?'yes':'no');
          $.post("/login/submit/", { name: usname, pass: uspass, checkbox: checked, template_framed:"yes",submit: "yes" }, function(data)
 {


     if(data=='error'){
       alert("<?=$lang['made_error']?>");

     }
     else{
        alert("Loged in");
     }
  });


}

 </script> 

它有效:)