Javascript 如何将两个相同的jQuery脚本合并到一个脚本中?
下面是我用来在数据库中添加一些条目的jQuery。它工作正常。我在同一个文件中使用同一个来删除一些数据。remove from db的形式具有idJavascript 如何将两个相同的jQuery脚本合并到一个脚本中?,javascript,jquery,Javascript,Jquery,下面是我用来在数据库中添加一些条目的jQuery。它工作正常。我在同一个文件中使用同一个来删除一些数据。remove from db的形式具有idRemoveMe,并将其发布到$.post(“add to db.php?id=“ 下面您可以看到添加到db脚本,其表单id为AddMe 我的问题是,是否有一种方法可以将这两者结合在一起。谢谢 $(function(){ $("#AddMe").submit(function(e){ e.preventDefault();
RemoveMe
,并将其发布到$.post(“add to db.php?id=“
下面您可以看到添加到db脚本,其表单id为AddMe
我的问题是,是否有一种方法可以将这两者结合在一起。谢谢
$(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>
$(函数(){
$(“#AddMe”).submit(函数(e){
e、 预防默认值();
$.post(“add to db.php?id=,$(“#AddMe”).serialize(),函数(数据){
$(“#提交”).attr('src','http://download.oracle.com/tech/blaf/specs/buttons/actnavbuttons/actnav_disabled_example.gif');
$(“#提交”).attr('disabled',true);
$(“#提交”).unbind('click');
$(“#message_post”).html(“谢谢”);
setTimeout(函数(){$(“#message_post”).hide();window.location.href=“product.php?id=“;”},2000);
});
});
});
$(函数(){
$(“#删除”).submit(函数(e){
e、 预防默认值();
$.post(“remove from db.php?id=,$(“#RemoveMe”).serialize(),函数(数据){
$(“#提交”).attr('src','http://download.oracle.com/tech/blaf/specs/buttons/actnavbuttons/actnav_disabled_example.gif');
$(“#提交”).attr('disabled',true);
$(“#提交”).unbind('click');
$(“#message_post”).html(“谢谢”);
setTimeout(函数(){$(“#message_post”).hide();window.location.href=“product.php?id=“;”},2000);
});
});
});
按上面的方式使用,然后删除此项:
});
$(function(){
按上面的方式使用,然后删除此项:
});
$(function(){
是的,您可以将其转换为:
$( function () {
// code block A
});
$( function () {
// code block B
});
为此:
$( function () {
// code block A
// code block B
});
是的,您可以将其转换为:
$( function () {
// code block A
});
$( function () {
// code block B
});
为此:
$( function () {
// code block A
// code block B
});
可以使用对象文本数组来存储id和url对
$(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);
});
});
});
});
$(函数(){
变量按钮=[
{id:'#AddMe',url:'addtodb.php'},
{id:'#RemoveMe',url:'remove from db.php'}
];
$。每个按钮、功能(i、v){
$(v.id).提交(函数(e){
e、 预防默认值();
$.post(v.url+“?id=,$(v.id).serialize(),函数(数据){
$(“#提交”)
.attr('src','http://download.oracle.com/tech/blaf/specs/buttons/actnavbuttons/actnav_disabled_example.gif')
.attr('disabled',true)
.unbind('click');
$(“#message_post”).html(“谢谢”);
setTimeout(函数(){
$(“#message_post”).hide();
window.location.href=“product.php?id=”;
}, 2000);
});
});
});
});
您可以使用对象文本数组来存储id和url对
$(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);
});
});
});
});
$(函数(){
变量按钮=[
{id:'#AddMe',url:'addtodb.php'},
{id:'#RemoveMe',url:'remove from db.php'}
];
$。每个按钮、功能(i、v){
$(v.id).提交(函数(e){
e、 预防默认值();
$.post(v.url+“?id=,$(v.id).serialize(),函数(数据){
$(“#提交”)
.attr('src','http://download.oracle.com/tech/blaf/specs/buttons/actnavbuttons/actnav_disabled_example.gif')
.attr('disabled',true)
.unbind('click');
$(“#message_post”).html(“谢谢”);
setTimeout(函数(){
$(“#message_post”).hide();
window.location.href=“product.php?id=”;
}, 2000);
});
});
});
});
可以,但这是最好的解决方案吗?可以,但这是最好的解决方案吗?