|
 |
|
สอบถามเรื่องการใช้ jquery ในการทำ ajax request คะ เมื่อ ajax call กลับมาแล้ว jquery ตัวอื่นไม่ทำงานคะ |
|
 |
|
|
 |
 |
|
Code (JavaScript)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="jquery-latest.pack.js"></script>
<script type="text/javascript" src="jquery-1.7.2.js"></script>
<script>
$(document).ready(function(){
var checkbox = function() {
$('.checkAll').click(function() {
if(this.checked){
$(':checkbox').attr("checked",true);
}else
{
$(':checkbox').attr("checked",false);
}
});
$('.cb-element').live('change', function() {
$('.cb-element').length == $('.cb-element:checked').length?
$('.checkAll').attr('checked',true):
$('.checkAll').attr('checked',false);
});
}
checkbox();
$('#N').change(function() {
var N = $(this).attr('value');
var Y = $('#Y').attr('value');
alert(N + Y);
$("#sectionid option").remove();
$.ajax({
type: "GET",
url: "getnews.php",
data: {"N":N,"Y":Y,"rand":Math.random()},
success: function(msg)
{
checkbox();
$("#txtHint").html(msg);
}
});
});
$('#Y').change(function() {
var N = $('#N').attr('value');
var Y = $(this).attr('value');
alert(N + Y);
$("#sectionid option").remove();
$.ajax({
type: "GET",
url: "getnews.php",
data: {"N":N,"Y":Y,"rand":Math.random()},
success: function(msg)
{
checkbox(); // เมื่อ ajax call กลับมาแล้ว function นี้ ไม่ยอมทำงานคะ
$("#txtHint").html(msg);
}
});
});
});
Tag : MySQL, Ajax, jQuery

|
|
 |
 |
 |
 |
Date :
2013-01-09 22:17:52 |
By :
jang |
View :
1126 |
Reply :
11 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองเอาฟังก์ชั่น checkbox ไว้ด้านนอก $(document).ready(function(){}); ดูครับ (ที่เหลือวางไว้เหมือนเดิม)
ลองทดสอบดูก่อนว่าเป็นอย่างไร
Code (JavaScript)
$(document).ready(function(){
.....
});
var checkbox = function() {
$('.checkAll').click(function() {
if(this.checked){
$(':checkbox').attr("checked",true);
}else
{
$(':checkbox').attr("checked",false);
}
});
$('.cb-element').live('change', function() {
$('.cb-element').length == $('.cb-element:checked').length?
$('.checkAll').attr('checked',true):
$('.checkAll').attr('checked',false);
});
}
|
ประวัติการแก้ไข 2013-01-09 23:10:35
 |
 |
 |
 |
Date :
2013-01-09 23:08:10 |
By :
dreamlover |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (JavaScript)
<script type="text/javascript" src="jquery-latest.pack.js"></script>
<script type="text/javascript" src="jquery-1.7.2.js"></script>
<script>
$(document).ready(function(){
checkbox();
$('#N').change(function() {
var N = $(this).attr('value');
var Y = $('#Y').attr('value');
alert(N + Y);
$.ajax({
type: "GET",
url: "getnews.php",
data: {"N":N,"Y":Y,"rand":Math.random()},
success: function(msg)
{
checkbox();
$("#txtHint").html(msg);
}
});
});
$('#Y').change(function() {
var N = $('#N').attr('value');
var Y = $(this).attr('value');
alert(N + Y);
$("#sectionid option").remove();
$.ajax({
type: "GET",
url: "getnews.php",
data: {"N":N,"Y":Y,"rand":Math.random()},
success: function(msg)
{
$("#txtHint").html(msg);
checkbox();
}
});
});
});
var checkbox = function() {
$('.checkAll').click(function() {
if(this.checked){
$(':checkbox').attr("checked",true);
}else
{
$(':checkbox').attr("checked",false);
}
});
$('.cb-element').live('change', function() {
$('.cb-element').length == $('.cb-element:checked').length?
$('.checkAll').attr('checked',true):
$('.checkAll').attr('checked',false);
});
}
</script>
|
 |
 |
 |
 |
Date :
2013-01-10 13:11:44 |
By :
jang |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองแล้ว ไม่ได้ คะ มีวิธีอื่นไหมคะ
|
 |
 |
 |
 |
Date :
2013-01-10 13:15:32 |
By :
jang |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองเช็คใน Console ดูครับ ว่าเกิดแจ้ง Error อะไรตอนที่รับ Event Select All
|
 |
 |
 |
 |
Date :
2013-01-10 13:25:14 |
By :
ALTELMA |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เช็ค ไม่ เป็น เลย คะ
|
 |
 |
 |
 |
Date :
2013-01-10 13:44:48 |
By :
jang |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใช้ Browser อะไรครับ?
|
 |
 |
 |
 |
Date :
2013-01-10 14:58:07 |
By :
ALTELMA |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
chrome คะ
|
 |
 |
 |
 |
Date :
2013-01-10 15:14:21 |
By :
jang |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อาการแบบนี้ผมเคยเป็นเหมือนกันครับ
เหมือนว่า มัน Callback ได้ครั้งเดี่ยว
แต่ผมแก้โดนการทำ function ไว้ด้านนอกของ $(document).ready()
|
 |
 |
 |
 |
Date :
2013-01-10 16:16:12 |
By :
Ex-[S]i[L]e[N]t |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ครับ แก้ตาม ที่คุณ Ex-[S]i[L]e[N]t แนะนำครับ ^ ^
|
 |
 |
 |
 |
Date :
2013-01-10 17:44:35 |
By :
ALTELMA |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองเอา 'var checkbox =' ออก แล้วเิพิ่มชื่อให้กับฟังก์ชั่นแทนดูครับ 'function checkbox() {}'
Code (JavaScript)
$(document).ready(function(){
.....
});
function checkbox() {
$('.checkAll').click(function() {
if(this.checked){
$(':checkbox').attr("checked",true);
}else
{
$(':checkbox').attr("checked",false);
}
});
$('.cb-element').live('change', function() {
$('.cb-element').length == $('.cb-element:checked').length?
$('.checkAll').attr('checked',true):
$('.checkAll').attr('checked',false);
});
}
หรือถ้าไม่อยากเปลียนโค้ด ก็ให้เอา var = checkbox = function() {} ไปไว้ด้านบนของ $(document).ready(function(){} แทน
Code (JavaScript)
var checkbox = function() {
$('.checkAll').click(function() {
if(this.checked){
$(':checkbox').attr("checked",true);
}else
{
$(':checkbox').attr("checked",false);
}
});
$('.cb-element').live('change', function() {
$('.cb-element').length == $('.cb-element:checked').length?
$('.checkAll').attr('checked',true):
$('.checkAll').attr('checked',false);
});
}
$(document).ready(function(){
.....
});
|
ประวัติการแก้ไข 2013-01-11 18:56:37
 |
 |
 |
 |
Date :
2013-01-11 18:55:29 |
By :
dreamlover |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เพราะ ใช้ก่อนจะ callback
$("#txtHint").html(msg);
checkbox();
-------------------------------
$("#txtHint").html(msg,function(){ checkbox(); });
|
ประวัติการแก้ไข 2013-01-11 19:11:40
 |
 |
 |
 |
Date :
2013-01-11 19:08:55 |
By :
t-monroe |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 05
|