2011-11-14 4 views
0

以下に、DBにいくつかのエントリを追加するために使用するjQueryがあります。正常に動作しています。同じファイル内の同じファイルを使用してデータを削除します。 削除-から-DBの形式は、ID RemoveMeがあり、フォームID AddMeアドオンに-DBスクリプトを見ることができるの下には$.post("add-to-db.php?id=<?php echo (int)$_GET['id'] ?>"2つの同一のjQueryスクリプトを1つにマージするには?

にポストします。

私の質問は、これら2つを1つにする方法があるかどうかです。ありがとうございました。

$(function(){ 
    $("#AddMe").submit(function(e){ 
     e.preventDefault(); 
     $.post("add-to-db.php?id=<?php echo (int)$_GET['id'] ?>", $("#AddMe").serialize(), function (data){ 
      $("#submit").attr('src','http://download.oracle.com/tech/blaf/specs/buttons/actnavbuttons/actnav_disabled_example.gif'); 
      $("#submit").attr('disabled',true); 
      $("#submit").unbind('click'); 
      $("#message_post").html("Thank you"); 
setTimeout(function() { $("#message_post").hide(); window.location.href = "product.php?id=<?php echo (int)$_GET['id'] ?>"; }, 2000); 

     }); 
    }); 
}); 

<form id="AddMe"> 
<input type="image" name="submit" id="submit" src="http://blog.mirthlab.com/wp-content/uploads/2008/04/rollover-up.png"> 
</form> 
<div id="message_post"></div> 

$(function(){ 
    $("#RemoveMe").submit(function(e){ 
     e.preventDefault(); 
     $.post("remove-from-db.php?id=<?php echo (int)$_GET['id'] ?>", $("#RemoveMe").serialize(), function (data){   
      $("#submit").attr('src','http://download.oracle.com/tech/blaf/specs/buttons/actnavbuttons/actnav_disabled_example.gif'); 
      $("#submit").attr('disabled',true); 
      $("#submit").unbind('click'); 
      $("#message_post").html("Thank you"); 
setTimeout(function() { $("#message_post").hide(); window.location.href = "product.php?id=<?php echo (int)$_GET['id'] ?>"; }, 2000); 
     }); 
    }); 
}); 

<form id="RemoveMe"> 
<input type="image" name="submit" id="submit" src="http://blog.mirthlab.com/wp-content/uploads/2008/04/rollover-up.png"> 
</form> 
<div id="message_post"></div> 

答えて

3

あなたはこれで[OK]を

$(function() { 
    var buttons = [ 
     { id : '#AddMe', url : 'add-to-db.php' }, 
     { id : '#RemoveMe', url : 'remove-from-db.php' } 
    ]; 

    $.each(buttons, function(i,v) { 
     $(v.id).submit(function(e){ 
      e.preventDefault(); 
      $.post(v.url + "?id=<?php echo (int)$_GET['id'] ?>", $(v.id).serialize(), function (data) { 
       $("#submit") 
        .attr('src','http://download.oracle.com/tech/blaf/specs/buttons/actnavbuttons/actnav_disabled_example.gif') 
        .attr('disabled',true) 
        .unbind('click'); 

       $("#message_post").html("Thank you"); 

       setTimeout(function() { 
        $("#message_post").hide(); 
        window.location.href = "product.php?id=<?php echo (int)$_GET['id'] ?>"; 
       }, 2000); 

      }); 
     }); 
    }); 
}); 
0

あなたは上記のそれを持っているとしてそれを使用して、この削除:このに

$(function() { 
    // code block A 
}); 

$(function() { 
    // code block B 
}); 

}); 



$(function(){ 
+0

IDとURLのペアを格納するオブジェクトリテラルの配列を使用することができますが、これは最適なソリューションですか? – asotshia

0

はい、あなたがこれを変換することができ

$(function() { 
    // code block A 
    // code block B 
}); 
関連する問題