-1

如何使用每个函数或其他函数编写我在 jquery 中选择的每个表单提交(无需逐个编写表单 jquery 提交)?我对此进行了编码,但不起作用。

$("form#form_report_user,form#send_pm").submit(function(){
    $.post($(this).attr('action'), $(this).serialize(), function(data) {
    $($(this).parent()).html("<img src='../images/loader.gif' alt='loading...' />");
      switch (parseInt(data.succ_code)){
          case 1: $($(this).parent()).html(data.html);break;
          default: $($(this).parent()).html(data.html);
      }
    }, 'json');
    return false;
});

和 html :

    <div class="send_pm">
        <h1>....</h1>
        <form action="load/send_pm.php" method="post" id="send_pm">
        <input name="user_id" type="hidden" value="<?php echo $userid; ?>"  />
        <div class="cleaner"></div>
        <span style="font-size:12px;">Subject : </span><br />
        <input name="subject" type="text" style="margin-top: 10px; margin-bottom: 10px;" />
        <div class="cleaner"></div>
        <span style="font-size:12px;">Message : </span>
        <div class="cleaner"></div>
        <textarea name="msg" id="add_txtarea" style="margin-top: 10px; margin-bottom: 10px;width: 220px;height: 100px;"></textarea>
        <div class="cleaner"></div>
        <input type="submit" value="send" name="send" class="nice_button" />
        </form>
</div>
<div class="gold_transfer">
        <h1>...</h1>
        <span style="font-size:12px;">....</span><br>
        <form action="load/transfer_gold.php" method="post" id="transfer_gold">
        <input name="user_id_receive" type="hidden" value="<?php echo $userid; ?>"  />
        <div class="cleaner"></div>
        <img src="../images/gold-coin.png" style="width:25px; height: 25px; padding-top:10px;" />
        <input name="gold_total_transfer" id="gold_total" value="" type="text" style="margin-top: 10px; margin-bottom: 10px; width: 40px;" />
        <div class="cleaner"></div>
        <input type="submit" value="send" name="send" class="nice_button" />
        </form>
</div>
4

3 回答 3

4

尝试这个:

$("form").each(function () {
    var $this = $(this);
    var $parent = $this.parent();
    $this.submit(function () {
        $.post($this.attr('action'), $this.serialize(), function (data) {
            $parent.html("<img src='../images/loader.gif' alt='loading...' />");
            switch (parseInt(data.succ_code)) {
                case 1:
                    $parent.html(data.html);
                    break;
                default:
                    $parent.html(data.html);
            }
        }, 'json');
        return false;
    });
});
于 2013-04-13T08:07:06.530 回答
0

你是在加载时做的吗?

也感觉括号太多,而且里面的loading也是不正确的

$(function() {
  $("#form_report_user, #send_pm").submit(function(e){
    var $formDiv = $(this).parent();
    $formDiv.append("<img src='../images/loader.gif' alt='loading...' />");
    $.post($(this).attr('action'), $(this).serialize(), function(data) {
      switch (parseInt(data.succ_code)){
        case 1: $formDiv.html(data.html);break;
        default: $formDiv.html(data.html);
      }
    }, 'json');
    return false;
  });
});
于 2013-04-13T08:06:27.677 回答
0

这应该适合你:

$("form#form_report_user,form#send_pm").each(function(){
    $(this).submit(function(){
            ...
    }); 
});
于 2013-04-13T08:07:25.347 回答