简体   繁体   English

从jquery ajax调用时,输入表单不起作用

[英]input form does not work when call it from jquery ajax

Sorry but i am totally newbie for ajax and jQuery if i am asking ridiculous question. 对不起,但如果我问一个荒谬的问题,我完全是ajax和jQuery的新手。

Basically i've a form like below; 基本上我的形式如下;

<form id="login_form" >
<input type="text" id="inputEmail" name="username">
<input type="password" id="inputPassword" name="password">
<div id="MyDynamicDiv"></div>
<button id="signin" name="signin" type="submit">button</button>
</form>

<script>
$("#login_form").submit(function() {
             if ($("#login_form").valid()) {
                    $('.messagebox').slideUp('slow');
                    var data1 = $('#login_form').serialize();
                            $("button").button('loading');
                    $.ajax({
                        type: "POST",
                        url: "login/login.php",
                        data: data1,
                           dataType: 'json',
                        success: function(msg) {
if (msg.result == 12) {
$('#MyDynamicDiv').load('captcha.php');
}
if (msg.result == 1) {    
      $('.messagebox').addClass("success-message");
         $('.message').slideDown('slow');
            $('#alert-message').text("....");
                $('#login_form').fadeOut(5000);
                     window.location = "members.php"
                  } else { return false;}
</script>

and my captcha.php is 我的captcha.php是

<input type="text" name="captcha_code">
<img id="captcha" src="/test/securimage/securimage_show.php">
<a href="#" id="captcha_link" onclick="document.getElementById('captcha').src = '/test/securimage/securimage_show.php?' + Math.random(); return false">[ Different Image]</a>

also login/login.php includes 还有login / login.php包括

echo json_encode( array('result'=>12));

I am ok to call captcha.php and show up on the page however, form does not work for captcha input. 我可以调用captcha.php并显示在页面上但是,表单不适用于验证码输入。 Should i call it in different way that i could not find out anywhere what i should do exactly. 我应该以不同的方式打电话给我,我无法找到我应该做的事情。

Edit: it seems input value of captcha_code does not send/post correctly. 编辑:似乎captcha_code的输入值没有正确发送/发布。 If i use the captcha.php code embedded into login form, than it works. 如果我使用嵌入登录表单的captcha.php代码,那么它的工作原理。 Thanks in advance, 提前致谢,

It appears your captcha input is outside of the <form> element, therefore it will not be included in the POST data when you serialize() the form. 您的验证码输入似乎在<form>元素之外,因此在serialize()表单时它不会包含在POST数据中。 Move the input inside the form element. 在form元素内移动输入。

You seem to be missing some closing parenthesis and braces. 你似乎错过了一些右括号和括号。

Doing return false from the success function does nothing because of the async nature. 从success函数return false不会因为异步性质而无效。 Instead you should return false from the submit event, or prevent default. 相反,您应该从提交事件返回false,或者防止默认。

Corrected code with commented changes (assuming you've moved the captcha input): 使用注释更改更正代码(假设您已移动验证码输入):

$("#login_form").submit(function (e) { // capture event
    e.preventDefault(); // always prevent the form from submitting
    if ($("#login_form").valid()) {
        $('.messagebox').slideUp('slow');
        var data1 = $('#login_form').serialize();
        $("button").button('loading');
        $.ajax({
            type: "POST",
            url: "login/login.php",
            data: data1,
            dataType: 'json',
            success: function (msg) {
                if (msg.result == 12) {
                    $('#MyDynamicDiv').load('captcha.php');
                }
                if (msg.result == 1) {
                    $('.messagebox').addClass("success-message");
                    $('.message').slideDown('slow');
                    $('#alert-message').text("....");
                    $('#login_form').fadeOut(5000);
                    window.location = "members.php"
                }
            } // was missing
        }); // was missing
    } // was missing
}); // was missing

For future debugging, remember to check the console (F12) for errors, and indent your code because it makes missing braces more obvious. 为了将来的调试,请记住检查控制台(F12)是否有错误,并缩进代码,因为它会使支撑缺失更明显。 You can use the TidyUp feature of JSFiddle to auto indent it. 您可以使用JSFiddle的TidyUp功能自动缩进它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM