【问题标题】:How to know if the form is really serialized?如何知道表单是否真的被序列化了?
【发布时间】:2012-06-14 07:34:32
【问题描述】:

单击提交按钮后,我有此表单要序列化,

   <form class="cashier_forms" action="<?php echo base_url().'index.php/'.$this->uri->segment(1).'/'.$this->uri->segment(2)?>/addCashierCount" id="form_cashier_count" rel="ajax_message">
       <p id="cashier_count_p" class="hidden">
          <label for="recipe_id">Count</label>
          <input placeholder="Cashier count for this Recipe" class="all_curve_small cashier_inputs" name="cashier_count" id="cashier_id" />
          <input id="yield_recipe_id" type="text" name="yield_id_input" value="<?php echo $ayr_recipe->yieldID?>" />
          <input type="text" name="count_date" value="<?php echo $date_now?>" />
          <input type="submit" class="buttons count_submit" rel="form_<?php echo $this->tbl_count_name ?>" value="Add Count" />
       </p>
    </form>

这是我的 jquery,据说可以序列化我的表单..

   $('.count_submit').click(function(e){
      e.preventDefault();
      var form    = '#'+ $(this).attr('rel');
      var url  = $(form).attr('action');
      var target  = '#'+ $(form).attr('rel');
      $(target).slideUp();
      $.post(url, $(form).serialize(),function(data) {
         $(target).html(data).slideDown(function(){
            if(data == '<div class="ok">Added</div>'){
            setTimeout(refresh,3000)
            window.location = '<?php echo base_url()?>index.php/cashier/cashier_lo';
            }
         });
      });
    });

但是在处理操作时,它不存储任何数据,我认为表单没有被序列化..有人可以检查我是否错过了 sumthing 吗?

【问题讨论】:

    标签: jquery serialization


    【解决方案1】:

    这不会给你表格:

    var form = '#'+ $(this).attr('rel');
    

    这为您提供了提交按钮的rel 属性(顺便说一句,它甚至没有这样的属性)。然后在上面调用.serialize()方法就没有意义了。访问表格。请记住,在您的点击处理程序中, this 指向被点击的元素(也就是按钮,而不是表单)。所以:

    var form = $(this).closest('form');
    

    据说更好的是订阅表单的提交事件,而不是按钮的点击事件。如果用户在输入字段内按 Enter 会怎样?表单将被提交,您的点击处理程序将永远不会运行。

    所以让我们清理一下吧:

    $('#form_cashier_count').submit(function(e) {
        e.preventDefault();
        var form = $(this);
        var target = form.attr('rel');
        $(target).slideUp();
        $.post(this.action, form.serialize(), function(data) {
            $(target).html(data).slideDown(function() {
                if (data == '<div class="ok">Added</div>') {
                    setTimeout(refresh, 3000);
                    window.location = '<?php echo base_url()?>index.php/cashier/cashier_lo';
                }
            });
        });
    });
    

    【讨论】:

    • 那么您在寻找什么?您在使用此代码时遇到了什么问题?我解释了为什么 $(form).serialize() 在你的代码中没有做任何事情。
    • 我不知道,但我实现了您刚才告诉我的操作,但它仍然无法正常工作。我的按钮现在变得无法点击..
    • 可能是因为向上滑动? AJAX 请求是否正在发送到您的服务器?使用 javascript 调试工具(例如 FireBug)来检查请求。
    • 不,一点也不……以前,在不使用您提供的编辑代码的情况下,请求正在发送到服务器,但是当我使用您的代码时,按钮现在变得不可点击。
    • 也许你有其他脚本禁用它?
    猜你喜欢
    • 1970-01-01
    • 2011-02-01
    • 2011-03-19
    • 1970-01-01
    • 1970-01-01
    • 2018-03-02
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    相关资源
    最近更新 更多