【问题标题】:jquery one function to send all forms and fetch datajquery 一个函数来发送所有表单并获取数据
【发布时间】:2011-10-13 15:37:14
【问题描述】:

所以我的目标是在我的 jQuery 代码中添加一个函数,它允许我执行 case 语句,然后允许我对每个表单执行我需要的操作,而无需重新键入整个 post 函数。

更新我的修复在这个问题的最底部

例如

    function fetch(e,formstring)
{

    $.ajax({
      type: 'POST',
      url: 'system/classes/core.php',
      data: formstring,
      dataType: 'json',
      contentType: "application/x-www-form-urlencoded;charset=utf-8",
      success: function(data){
          $.each(data, function(i, obj) {

               switch (e) { 
                 case 1:
                  $.each(obj, function(key, val) {
                     alert(key+" - "+val);
                  });
                 break;
                 case 2:
                    alert("sucker");
                 break;
                 case 3:
                 //LoginSript

                    $('#rightheader').html(obj.code);
                 break;
                 case 4:
                 //WelcomePage/Signup
                    $('#window').html(obj.code);
                 break;
               }
         });
      },
      error: function(data){
      $.each(data,function(i,myinfo){
            alert(i);       
          });
      }
    });
    return false;
}

虽然获取数据似乎可行,但提交表单似乎不行。

$(function() {

    $("form#login").submit(function(){
        shownotify(1,"Please hold we are login you in.");
        fetch(1,$(this).serialize());   
    });
});

我认为我做的一切都是正确的。

您也可以在这里看到显示通知功能。

    function shownotify(e,msg)
{
    if(e==1){$('#notify').show();};
    if(e==2){$('#notify').hide();};

    $('#notifyheader').html("Please Hold");
    $('#notifytext').html(msg); 
}

我想知道我是否有错误,还是我做错了?

【问题讨论】:

  • 请将您的解决方案添加到支持者answer,否则其他人不会知道此问题已解决。
  • 完成不知道我可以回答自己的问题

标签: jquery ajax forms post submit


【解决方案1】:

我修复错误的方法是在 $(function(){}) 周围设置一个 setTimeout 函数

setTimeout(function(){
$(function() {

    $("form#login").submit(function(){
        shownotify(1,"Please hold we are login you in.");
        fetch(2,$(this).serialize());   
    });
});
},1000);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-25
    • 2013-01-10
    • 2021-07-21
    • 2023-03-20
    • 2017-01-05
    相关资源
    最近更新 更多