jQuery JS:使用两个表单提交并决定要做什么

时间:2010-11-17 14:05:41

标签: jquery ajax forms

function removeComment(bID) {
    $('#submitChangeComment').attr('disabled', true);
    $.ajax({ 
        type: "POST",
        url: "misc/changeComment.php",
        data: {
            mode: 'del',
            bID : bID,
            comment : $('input[name=Comment]:visible').val() 
        }, 
        success: function(msg){
            $('#submitChangef').attr('disabled', false);
            $('#currentComment' + bID).hide();

            var $msg = $("#nowCurrentComment" + bID).find('.comment');
            // if it already has a comment, fade it out, add the text, then fade it back in
            if ($msg.text().length) {
                $msg.fadeOut('fast', function() {
                    $msg.text(msg).fadeIn('fast');
                });
            } else {
                // otherwise just hide it, add the text, and then fade it in
                $msg.hide().text(msg).fadeIn('fast'); 
            }
        }
    });
}

function FriendChangeComment(bID) {
    $('#submitChangef').attr('disabled', true);
    $.ajax({ 
        type: "POST",
        url: "misc/changeComment.php",
        data: {
            mode: 'edit',
            bID : bID,
            comment : $('input[name=Comment]:visible').val() 
        }, 
        success: function(msg) {
            $('#submitChangef').attr('disabled', false);
            $('#currentComment' + bID).hide();

            var $msg = $("#nowCurrentComment" + bID).find('.comment');
            // if it already has a comment, fade it out, add the text, then fade it back in
            if ( $msg.text().length ) {
                $msg.fadeOut('fast', function() {
                    $msg.text( msg ).fadeIn('fast');
                });
            } else {
                // otherwise just hide it, add the text, and then fade it in
                $msg.hide().text( msg ).fadeIn('fast'); 
            }
        }
    });
}

形式:

<form action="javascript:FriendChangeComment(<? echo $showInfo["bID"]; ?>)" method="post">
change comment for <br><?php echo fullname($showInfo["bID"]); ?>:<br>
<input type="text" name="Comment" value="<?=$showC["comment"];?>" size="20">  
<input name="submit" type="submit" id="submitChangef" value="Save">
<input name="removeC" type="button" id="submitremoveComment" value="remove" onClick="">
</form>

我在一个表单中有两个提交按钮。如果你点击第一个“submitChangef”我希望它运行FriendChangeComment(),如果你点击“removeComment”我希望它运行removeComment。两者都是相同的函数,它们之间的区别仅在于ajax数据中的模式:(del,edit)。我不知道如何缩短代码并简化代码,因为它们都是彼此完全相同的(差不多)。

如何做到这一点?

2 个答案:

答案 0 :(得分:1)

bID作为隐藏输入字段添加到表单并将事件绑定移动到jQuery而不是在表单本身中执行,然后将bID和mode作为参数传递给函数:

$('#submitChangef').click(function() {
    bID = $(this).closest('form').find('#bID').val();
    changeComment(bID, 'edit');
    return false;
});

$('#submitremoveComment').click(function() {
    bID = $(this).closest('form').find('#bID').val();
    changeComment(bID, 'del');
    return false;
});

function changeComment(bID, mode) {
    $('#submitChangeComment').attr('disabled', true);
    $.ajax({ 
        type: "POST",
        url: "misc/changeComment.php",
        data: {
            mode: mode,
            bID : bID,
            comment : $('input[name=Comment]:visible').val() 
        }, 
        success: function(msg){
            $('#submitChangef').attr('disabled', false);
            $('#currentComment' + bID).hide();

            var $msg = $("#nowCurrentComment" + bID).find('.comment');
            // if it already has a comment, fade it out, add the text, then fade it back in
            if ($msg.text().length) {
                $msg.fadeOut('fast', function() {
                    $msg.text(msg).fadeIn('fast');
                });
            } else {
                // otherwise just hide it, add the text, and then fade it in
                $msg.hide().text(msg).fadeIn('fast'); 
            }
        }
    });
}

答案 1 :(得分:0)

我会根据您的需要使用一个函数和一个隐藏的形式参数,例如: 形式:

<form id='myform' method="post">
change comment for <br><?php echo fullname($showInfo["bID"]); ?>:<br>
<input type="text" name="Comment" value="<?=$showC["comment"];?>" size="20">  
<input name="submit" type="submit" onclick="processBtnClick(this, 'edit');" value="Save">
<input name="removeC" type="button" onclick="processBtnClick(this, 'remove');" value="remove" onClick="">
<input type='hidden' name='action' id='action'/>
</form>

JS:

function processBtnClick(bID, action){
  $('#action').value(action);
  $('#myform').submit();

/ *或您的.ajax代码   $就({        类型:“POST”,        url:“misc / changeComment.php”,        ... * /     }

php(或其他):

 if ($_POST['action']=='remove')
      do_remove_stuff();
    else
      do_edit_stuff();