나는이 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 functio로 바꾸길 원합니다. 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 함수에 전달하는 정확하고 실현 가능한 방법은 무엇입니까?
는userOperation
에 함수를 전달하는, 하나
대부분의 코드가 외계인처럼 보입니다. D, 유감 스럽지만 jQuery에서 초보자입니다. 입찰가와 정확히 무슨 일이 일어나는지에 대해 설명하여 도움을 주시면 감사하겠습니다. –
@ibrahim : 저는 그냥 당신을 위해 그것을 주석. ;-) –
그 종류의 당신, 고마워요 :) –