【问题标题】:Jquery .live passes parameter twice in requestJquery .live 在请求中两次传递参数
【发布时间】:2013-12-25 09:21:51
【问题描述】:

我正在创建简单的登录屏幕,它会在单击提交按钮时接受用户名和密码。我尝试使用 .click 但它根本不起作用,所以我使用的是 .live 但它在请求中两次传递相同的参数。

$("#loginsubmit").live('click', function (e) {
     $.ajax({
         url: 'auth_check.php',
         data: $(loginForm1).serialize(),
         type: 'POST',
         cache: false,
         success: function (result) {
             if (result == 'success') {}
         },
         error: function (result) {}
     });
     e.preventDefault();
     $.prettyPhoto.close();
     return false;
 });

这是我收到的请求

pusername=&ppassword=&pusername=abc&ppassword=abc

我第一次使用 jquery,在我的模板中使用了一个叫做漂亮照片的东西。 我猜这张漂亮的照片可能会引起问题。

LoginForm1 如下所示:

<div class="login_register_stuff hide"><!-- Login/Register Modal forms - hidded by default to be opened through modal -->
    <div id="login_panel">
        <div class="inner-container login-panel">
            <h3 class="m_title">SIGN IN YOUR ACCOUNT TO HAVE ACCESS TO DIFFERENT FEATURES</h3>
            <form  name="loginForm1" method="post" enctype="multipart/form-data" />
                <a href="#" class="create_account" onclick="ppOpen('#register_panel', '280');">CREATE ACCOUNT</a>
                <input type="text" name="pusername" class="inputbox" placeholder="Username" />
                <input type="password"  name="ppassword" class="inputbox" placeholder="Password" />
                <input type="submit"  id="loginsubmit" name="loginsubmit"   value="LOG IN" />
            </form>
            <div class="links"><a href="#" onclick="ppOpen('#forgot_panel', '350');">FORGOT YOUR USERNAME?</a> / <a href="#" onclick="ppOpen('#forgot_panel', '350');">FORGOT YOUR PASSWORD?</a></div>
        </div>

漂亮的照片部分sn-p

enter  <!-- prettyphoto scripts & styles -->

function ppOpen(panel, width){
    jQuery.prettyPhoto.close();
    setTimeout(function() {
        jQuery.fn.prettyPhoto({social_tools: false, deeplinking: false, show_title: false, default_width: width, theme:'pp_kalypso'});
        jQuery.prettyPhoto.open(panel);
    }, 300);
} // function to open different panel within the panel

jQuery(document).ready(function($) {

    jQuery("a[data-rel^='prettyPhoto'], .prettyphoto_link").prettyPhoto({theme:'pp_kalypso',social_tools:false, deeplinking:false});
    jQuery("a[rel^='prettyPhoto']").prettyPhoto({theme:'pp_kalypso'});
    jQuery("a[data-rel^='prettyPhoto[login_panel]']").prettyPhoto({theme:'pp_kalypso', default_width:800, social_tools:false, deeplinking:false});


    jQuery(".prettyPhoto_transparent").click(function(e){

        e.preventDefault();
        jQuery.fn.prettyPhoto({social_tools: false, deeplinking: false, show_title: false, default_width: 980, theme:'pp_kalypso transparent', opacity: 0.95});
        jQuery.prettyPhoto.open($(this).attr('href'),'','');
    });

});

这里

你能帮我找出问题吗?

【问题讨论】:

  • 至少使用 .on - .live 已弃用。表单是什么样子的,它在 chrome 的检查元素中是什么样子的?
  • loginForm1 在您的代码中是什么样的?
  • e 在您的代码中是 undefined。您的点击处理程序应该接受事件对象,然后您就可以使用它了。
  • @BlackSheep:会的,但是代码甚至没有达到 preventDefault,它的 ajax 请求还没有完成
  • 我认为您将 name 属性放在与输入名称相同的标签上

标签: php jquery prettyphoto


【解决方案1】:

这是因为 prettyphoto 通过打开来克隆您的内联内容。打开内联内容后,您必须清除原始内容 - 否则您将获得所有内容两次 - 如果关闭 prettyphoto,如果它不是动态生成的,则必须将其放回原处。 例子: $.fn.prettyPhoto({ ajaxcallback:function(){ $("#yourhiddenformid").html(""); }, callback:function(){ no need, if its dynamic generated, otherwise: rewrite html of div; }

【讨论】:

    【解决方案2】:

    你必须在表单上绑定 onsubmit 事件:

    $("#loginForm1").on('submit', function (e) {
         $.ajax({
             url: 'auth_check.php',
             data: $('#loginForm1').serialize(),
             type: 'POST',
             cache: false,
             success: function (result) {
                 if (result == 'success') {}
             },
             error: function (result) {}
         });
         e.preventDefault();
         $.prettyPhoto.close();
         return false;
    });
    

    【讨论】:

    • 我试过了,但它不起作用。在提交事件未触发时,我尝试将警报放入函数中。它只是刷新页面。
    • @user3134221,尝试在 javascript 控制台中执行:$('#loginForm1').length - 它的长度是多少?
    • 另外,你包含了多少次 jquery?
    • 我没有收到这个问题,因为我是 jquery 新手,但在我的文件中有两个地方,一个是这个漂亮的照片部分 --> jQuery(document).ready(function($) { .. .................... });第二个是 (function($){ $(document).ready(function(){ ....my events..... }); })(jQuery);
    • 查看页面来源并计算次数
    猜你喜欢
    • 1970-01-01
    • 2020-12-22
    • 1970-01-01
    • 2013-03-12
    • 1970-01-01
    • 1970-01-01
    • 2011-07-05
    • 2021-09-24
    • 1970-01-01
    相关资源
    最近更新 更多