การอ้างชื่อใน jquery
ถาม-ตอบ แนะนำไอเดียว โค้ดตัวอย่าง แนวทาง วิธีแก้ปัญหา การอ้างชื่อใน jquery
การอ้างชื่อใน jquery
1 2 3 4 5 | hint.html ส่งค่าไป gethint.php เพื่อค้นหารายชื่อมาแสดงที่ hint.html จากนั้นคลิกที่รายชื่อเพื่อให้เป็นแถบสีแดง ปัญหาของผมคือมันไม่เป็นแถบสีแดง ไม่ทราบว่าผมอ้างชื่อหรือทำอะไรผิดหรือเปล่าครับ ช่วยตรวจสอบให้หน่อยครับ ขอบคุณครับ < strong >hint.html</ strong > |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <!DOCTYPE html> < html > < head > < title >test jQuery</ title > < script type = "text/javascript" src = "../js/jquery.js" ></ script > < script language = "javascript" > $(document).ready(function(){ $('button').click(function(){ str=$('#txt_name').val(); // ajax (get) $.get('gethint.php',{q:str},function(result){ $('#ajax_result').html(result); }); }); $('li').click(function(){ $(this).css('background-color','red'); }); }); </ script > </ head > < body > < input id = "txt_name" type = "text" onKeyUp = "showHint(this.value)" > < button >click</ button > < br >< br > < ul id = "ajax_result" > </ ul > </ body > </ html > |
1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <strong>gethint.php </strong><? //$q=$_GET["q"]; $q =iconv( "UTF-8" , "windows-874" , $_GET [ 'q' ]); $sql = "SELECT * FROM employee_user WHERE name_en like '" . $q . "%' and remark<>'*' order by name_en" ; $con = mysql_connect( 'localhost' , 'username' , 'pwd' ); if (! $con ) { die ( 'Could not connect: ' . mysql_error()); }mysql_select_db( "employee" , $con ); $result = mysql_query( $sql ); echo "<p>" . $sql . "</p>" ; $return_arr = array (); while ( $row = mysql_fetch_array( $result )){ echo "<li>" . $row [ "name_en" ]. " " . $row [ "lastname_en" ]. "</li>" ; } mysql_close( $con ); ?> |

คำแนะนำ และการใช้งาน
สมาชิก กรุณา ล็อกอินเข้าระบบ เพื่อตั้งคำถามใหม่ หรือ ตอบคำถาม สมาชิกใหม่ สมัครสมาชิกได้ที่ สมัครสมาชิก
- ถาม-ตอบ กรุณา ล็อกอินเข้าระบบ
ความคิดเห็นที่
1
น่าจะเป็นเพราะว่า คุณใส่ code บรรทัดนี้ไว้ก่อน
$('li').click(function(){
$(this).css('background-color','red');
});
โดยใส่ไว้ที่ onReady ซึ่งเป็นช่วงที่ยังไม่กด Click แปลว่า ไม่มี <li> เลยซัก 1 element
แต่ code สั่งให้ bind แล้ว แล้วจับ event
พอกด click มา เจ้า li ที่เพิ่ง มา มันยังไม่ถูก bind อะครับ
ดังนั้นคุณต้อง ใส่ code นี้
$('li').click(function(){
$(this).css('background-color','red');
});
ตอนกด click ของ button อะคับ
มันไม่เหมือน CSS ที่ ใส่ก่อนได้ อะไรที่มาหลัง ก็ยังมีผล อิอิ
แต่ javascript มันจะ จับ element ก่อน ครับ

ขอบคุณทุกการสนับสนุน
![]()