我有这个jQuery的事件处理程序代码,我想将它转换成一个功能这个jQuery函数有什么问题?
$("#activate-user").click(function() {
var userId = [];
$(':checkbox:checked').each(function(i) {
userId[i] = $(this).val();
});
if(userId.length == 0) {
//If userId is empty
} else {
$.ajax({
type: 'POST',
url: 'process.php',
data: 'option=activate&userId='+userId,
success: function(msg){
if(msg == 0) {
//If cannot process the query.
} else {
//If query is processed.
location.reload(true);
}
}
});
}
});
$("#block-user").click(function() {
var userId = [];
$(':checkbox:checked').each(function(i) {
userId[i] = $(this).val();
});
if(userId.length == 0) {
//If userId is empty
} else {
$.ajax({
type: 'POST',
url: 'process.php',
data: 'option=block&userId='+userId,
success: function(msg){
if(msg == 0) {
//If cannot process the query.
} else {
//If query is processed.
location.reload(true);
}
}
});
}
});
这两个事件处理程序之间的唯一区别是,即option=
data: 'option=activate&userId='+userId
data: 'option=block&userId='+userId
值
我想这个把它转换成jQuery函数N代表我试图做这样它完全不工作,
(function ($) {
jQuery.fn.userOperation = function(opString) {
this.click(function() {
var userId = [];
$(':checkbox:checked').each(function(i){
userId[i] = $(this).val();
});
if(userId.length === 0) {
//If userId is empty.
} else {
$.ajax({
type: 'POST',
url: 'process.php',
data: 'option='+opString+'&userId='+userId,
success: function(msg){
if(msg == 0) {
//If cannot process the query.
} else {
//If query is processed.
location.reload(true);
}
}
});
}
});
}
}(jQuery));
jQuery的函数调用:
$('#activate-user').userOperation(function() {
return 'block';
});
这似乎并不工作,因为我没有正确地传递参数给函数,因为我是jQuery的新手,我不知道如何做到这一点。我哪里错了?这是将参数传递给jQuery函数的正确和可行的方法?
谢谢你..
大部分的代码看起来外国人对我来说:D,我很抱歉,但我是jQuery的新手,我会很感激,如果你可以扩大你的帮助,解释我到底是怎么回事:) –
@ibrahim:I只是为你评论它。 ;-) –
那真是太好了,谢谢:) –