代码之家  ›  专栏  ›  技术社区  ›  ayush

使用jquery显示成功的表单提交

  •  0
  • ayush  · 技术社区  · 14 年前

    我有一个名为优惠券.js-

    jQuery(document).ready(function(){
            jQuery('.appnitro').submit( function() {
    $.ajax({
                url     : $(this).attr('action'),
                type    : $(this).attr('method'),
                dataType: 'json',
                data    : $(this).serialize(),
                success : function( data ) {
                            for(var id in data) {
                                jQuery('#' + id).html( data[id] );
                            }
                          }
    
            });
    
            return true;
        });
    
    });
    

    短信.php-

    <?php
    //process form
    $res = "message deliverd";
    $arr = array( 'content' => $res );
    echo json_encode( $arr );//end sms processing
    unset ($_POST);
    ?>
    

    我是这样打电话的-

        <form id="smsform" class="appnitro"  method="post" action="sms.php"> 
    ... 
    <input id="saveForm" class="button_text" type="submit" name="submit" value="Submit"/>
    </form>
    <div id="content"></div>
    

    {"content":"message deliverd"}
    

    请告诉我哪里出错了。我的javascript是正确的。firebug没有显示错误。 或者请告诉其他一些方法来实现所需的功能。

    jQuery(document).ready(function(e){
            jQuery('.appnitro').submit( function() {
    $.ajax({
                url     : $(this).attr('action'),
                type    : $(this).attr('method'),
                dataType: 'json',
                data    : $(this).serialize(),
                success : function( data ) {
                            for(var id in data) {
                                jQuery('#' + id).html( data[id] );
                            }
                          }
    
            });
    
    e.preventDefault();
        });
    
    });
    

    即使我在最后加上退货,也不起作用。 请建议一些其他的方法来实现这个功能

    2 回复  |  直到 14 年前
        1
  •  2
  •   Jacob Relkin    14 年前

    页面刷新的原因是 submit 事件没有被抑制。

    有两种方法:

    • preventDefault() 在上面。
    • return false

    对你修改过的问题的回答:你正在接受 e 参数在错误的函数中,应该在 提交 处理程序,而不是 ready 处理程序。

        2
  •  2
  •   kevtrout    14 年前

    我认为您需要取消jquery中的表单提交。从jQuery文档中:

    现在提交表单时 到实际提交,所以我们可以 通过调用 .preventDefault()在事件对象上 或者从我们的 处理程序。我们可以触发事件 当另一个元素

    所以在你的代码里:

    //add 'e' or some other handler to the function call   
     jQuery(document).ready(function(e){
                jQuery('.appnitro').submit( function() { $.ajax({
                    url     : $(this).attr('action'),
                    type    : $(this).attr('method'),
                    dataType: 'json',
                    data    : $(this).serialize(),
                    success : function( data ) {
                                for(var id in data) {
                                    jQuery('#' + id).html( data[id] );
                                }
                              }
    
                });
                //return false
                return false;
    
               //or
               e.preventDefault();
            });
    
        });