2012-05-03 26 views
0

大家好,我正在使用下面的代码在我的页面上运行ajax提交,并且怀疑它是否是处理它的最有效的方式,因为用于捕获用户数据的各种方法。运行ajax提交方法的其他方法

更好的方法需要什么?

这是JavaScript代码我目前使用:

$(document).ready(function() { 
    $("#submit").click(function() { 
     $("#form").hide(300); 
     $.post('run.php', $("#form").serialize(), 
     function(data) { 
      $("#result").html(data); 
     }); 
     return false; 
    }); 
}); 

这是我的形式:

<form id="booking"> 
      <table width="600" cellpadding="2px" cellspacing="2px" style="font-size: 20px;"> 
      <tr> 
       <th width="296" align="left">Date Of Wedding</th> 
       <td width="288"><input name='date' type='text' class='larger' id='date' value='' size='20'/></td> 
      </tr> 
      <tr> 
       <th align="left">Party Size</th> 
       <td><input name='partySize' type='text' class='larger' id='partySize' value='' size='20' /></td> 
      </tr> 
      <tr> 
       <th align="left">Catering Grade</th> 
       <td>1: 
       <input name='cateringGrade' type='radio' class='larger' value=1 size='12'/> 
       2: 
       <input name='cateringGrade' type='radio' class='larger' id='cateringGrade' value=2 size='12'/> 
       3: 
       <input name='cateringGrade' type='radio' class='larger' id='cateringGrade' value=3 size='12'/> 
       4: 
       <input name='cateringGrade' type='radio' class='larger' id='cateringGrade' value=4 size='12'/> 
       5: 
       <input name='cateringGrade' type='radio' class='larger' id='cateringGrade' value=5 size='12'/></td> 
      </tr> 
      <tr> 
       <th align="left">&nbsp;</th> 
       <td>&nbsp;</td> 
      </tr> 
      <tr> 
       <th align="left">&nbsp;</th> 
       <td><input name="submit" type="button" value="Submit" id="submit"/></td> 
      </tr> 
      </table> 
     </form> 

回答

1
$(document).ready(function() { 
    $("#submit").click(function() { 
     var form = $("#form"); 
     form.hide(300); 
     $.ajax({ 
      url: 'run.php', 
      type: form.attr('method'), // POST or GET, 
      data: form.serialize(), 
      success: function(data) { 
      // handle with response 
      $("#result").html(data); 
      } 
     }); 
     return false; 
    }); 
}); 

但最好做到在提交表单功能类似以下内容:

$(document).ready(function() { 
     $('form').submit(function(e) { 
      e.preventDefault(); 
      var form = $(this); 
      form.hide(300); 
      $.ajax({ 
       url: 'run.php', // you can also use form.attr('action') if url is dynamic 
       type: form.attr('method'), // POST or GET, 
       data: form.serialize(), 
       success: function(data) { 
       // handle with response 
       $("#result").html(data); 
       } 
      }); 
     }); 
    }); 
+0

删除或注释掉dateType json。他显然使用返回的数据作为html而不是对象值。我无法找到解析ajax()的请求数据的位置。 –

+0

@methusaleh。您已经有了一些很好的建议,但我也会使用'.ajax.beforeSend()'&'$ .ajax.error()'方法。 (''form')。on('submit',function(){...})'你可以隐藏表单“beforeSend” - 对ajax调用,如果有错误,你可以显示表格 –

1

$。员额()使用$。阿贾克斯()函数,这样既很适合。

我更喜欢$ .ajax(),因为我觉得它更合乎逻辑并且易于配置。

我想你的脚本改成这样:

$(document).ready(function() { 
    $("#form").bind('submit', function(e) { 
     e.preventDefault(); 
     $(this).hide(300); 
     $.ajax({ 
      type: 'POST', 
      url: 'run.php', 
      data: $("#form").serialize(), 
      success: function(data) { 
       $("#result").html(data); 
      } 
     }); 
    }); 
}); 
+0

我将如何设置'$。 ajax()'使用我上面的表单?你有样品吗? – methuselah

+0

给出的例子。我也改变了事件,因为我发现捕获表单提交更合理,而不是点击一个按钮。也许有人试图通过在输入字段中按Enter键来提交它? –