dongyi2889 2015-03-02 08:03
浏览 39

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

Sorry but i am totally newbie for ajax and jQuery if i am asking ridiculous question.

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

<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

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. 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. If i use the captcha.php code embedded into login form, than it works. Thanks in advance,

  • 写回答

1条回答 默认 最新

  • dongzhui2636 2015-03-02 08:19
    关注

    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. Move the input inside the form element.

    You seem to be missing some closing parenthesis and braces.

    Doing return false from the success function does nothing because of the async nature. Instead you should return false from the submit event, or prevent default.

    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. You can use the TidyUp feature of JSFiddle to auto indent it.

    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序