2010-11-17 17 views
0
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'); 
      } 
     } 
    }); 
} 

 jQuery的JS:使用兩個表單提交,並決定該怎麼做

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)。我不知道如何縮短代碼並簡化代碼,因爲它們都是彼此完全重複的(幾乎)。

這怎麼辦?

回答

1

爲隱藏輸入字段中添加bID到窗體和移動你的事件綁定到jQuery的,而不是在窗體本身做的,然後傳遞bID和模式一個函數的參數:

$('#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'); 
      } 
     } 
    }); 
} 
0

我會使用你的需求一個函數和一個隱藏的表單PARAM,如: 形式:

<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(); 

/*或您的阿賈克斯代碼 $。阿賈克斯({ 類型: 「POST」, 網址: 「雜項/ changeComment.php」, ... * /}

PHP(或別的東西):

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

id =「processBtnClick()? – Karem 2010-11-17 14:26:08

+0

對不起,當然「onclick」,而不是「id」 – heximal 2010-11-17 15:04:10