表格未提交

时间:2014-06-06 10:58:41

标签: jquery

我正在尝试提交表单,但它无效。在下面的代码中,当返回“成功”时,它甚至将“登录”提交按钮的颜色更改为红色。在Chrome控制台中,它不显示任何内容,并且在网络选项卡中它仅调用Ajax调用活动:

(function ($) {
    Drupal.behaviors.ajax_example = {
        attach:function (context) {
            jQuery('#user-login #edit-submit').click(

                function(){

                    var login = jQuery("#user-login #edit-name").val();
                    var pass = jQuery("#user-login #edit-pass").val();


                    //make Ajax call to get authentication details
                    jQuery.ajax({
                        type:'POST',
                        data:{
                            js: 1, 
                            login: login,
                            passwd:pass
                        },
                        dataType:'json',
                        success: function(data)
                        {            
                            if(data.msg)
                            {
                                alert(data.msg);

                                if(data.msg == "success")
                                {
                                    alert("submitted the form");
                                    jQuery('#user-login #edit-submit').css('color', 'red');
                                    jQuery('#user-login #edit-submit').submit();
                                }
                            }
                        },
                        url: "http://localhost/my/user/verifylogin"
                    });

                    //alert('sent data1');
                    return false;
                })
            return true;  
        }
    }
})(jQuery);

Drupal表单的html代码是:

<div id="content" class="column">
  <div class="section"> <a id="main-content"></a>
    <h1 class="title" id="page-title"> User account </h1>
    <div class="tabs"> </div>
    <div class="region region-content">
      <div id="block-system-main" class="block block-system">
        <div class="content">
          <form action="/my/user" method="post" id="user-login" accept-charset="UTF-8">
            <div>
              <div class="form-item form-type-textfield form-item-name">
                <label for="edit-name">Username <span class="form-required" title="This field is required.">*</span></label>
                <input type="text" id="edit-name" name="name" value="" size="60" maxlength="60" class="form-text required">
                <div class="description">please full name ending with @woodstock.ac.in</div>
              </div>
              <div class="form-item form-type-password form-item-pass">
                <label for="edit-pass">Password <span class="form-required" title="This field is required.">*</span></label>
                <input type="password" id="edit-pass" name="pass" size="60" maxlength="128" class="form-text required">
                <div class="description">Enter the password that accompanies your username.</div>
              </div>
              <input type="hidden" name="form_build_id" value="form-gJYws5bihrq8cGOg-bZXiTcd9u5JoTqA4mqiw8nW3jc">
              <input type="hidden" name="form_id" value="user_login">
              <div class="form-actions form-wrapper" id="edit-actions">
                <input type="submit" id="edit-submit" name="op" value="Log in" class="form-submit" style="color: red;">
              </div>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</div>

2 个答案:

答案 0 :(得分:0)

使用submit()代替click()

jQuery('#user-login').submit(....
  

当用户尝试时,提交事件将发送到元素   提交表格。

您正在寻找,而不是点击表单。

答案 1 :(得分:0)

$( "#other" ).click(function() {
    $( "#target" ).submit();
});