|
 |
|
ถ้าเราจะเรียก ฟั่งชั่นใน loop ทำยังไงครับ |
|
 |
|
|
 |
 |
|
function ก็เขียนแยกไว้ก็ถูกแล้วนิครับ เวลา loop ก็เรียกใช้แค่นั้น
ตัวอย่างง่ายๆน่ะครับ
Code (JavaScript)
function test(var)
{
return true;
}
for(i=0;i<=x;i++){
test(var);
}
|
 |
 |
 |
 |
Date :
2018-04-17 10:11:10 |
By :
mongkon.k |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองเอา return หน้า function ออกดูครับ
|
 |
 |
 |
 |
Date :
2018-04-17 10:48:08 |
By :
mongkon.k |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่ได้อ่ะครับ ลอง เอา return หน้า function ออกแล้วอ่ะครับ อันนี้โค๊ดที่ผมทำอ่ะครับ
Code (PHP)
<?php $test=5000;?>
<SCRIPT language=JavaScript>
function check_number(event)
{
var e=window.event?window.event:event;
var e_k=e.keyCode?e.keyCode:e.which?e.which:e.charCode;
if (!(e_k == 46 || e_k == 8 || e_k == 13 || (e_k>=96 && e_k<=105)||(e_k>=48 && e_k<=57)))
{
alert("ต้องเป็นตัวเลขเท่านั้น... \nกรุณาตรวจสอบข้อมูลของท่านอีกครั้ง...");
document.form2.money.value="";
document.form2.money.focus();
return false;
}
return true;
}
function check_number2()
{
var u_score=<?=$test?>;
if(document.form2.money.value >= u_score)
{
alert('คุณป้อนจำนวนเกินกว่าจำนวนที่คุณได้รับกรุณาป้อนใหม่ครับ');
document.form2.money.value="";
document.form2.money.focus();
return false;
}
}
</script>
<form method="post" name="form2">
<table class="table table-bordered" style="font-size: 12px; background-color: #f8f8f8;">
<th width="20"><input name="CheckAll" type="checkbox" id="CheckAll" value="Y" onClick="ClickCheckAll(this);"></th>
<!--<th style="padding: 0px 0px 0px; vertical-align:Middle;"><font color = 'red'>Accp</th>-->
<th style="padding: 0px 50px 0px; vertical-align:Middle;">Quotation</th>
<?php
$i =0;
while($resultpo=sqlsrv_fetch_array($querypo))
{
?>
<td><div class="form-group col-xl-100"><input type="text" name="money" id="textfield" onkeyup="return check_number2();" onkeydown="return check_number(event);" class="form-control">s</div></td>
<td><center><div class="form-group col-xl-100"><input type="text" name="invoice[]" id="invoice" value="<?php echo $resultpo["VINVNO"];?>" class="form-control"></div></center></td>
<?php
$i++;
}
?>
</table>
ประมาณนี้อ่ะครับ
|
 |
 |
 |
 |
Date :
2018-04-17 10:58:42 |
By :
owenback |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองกด f12 debug mode ใน chrome แล้วดู error ที่แท็ป Console ครับช่วยได้เยอะ

|
 |
 |
 |
 |
Date :
2018-04-17 11:11:06 |
By :
mongkon.k |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ทำให้มันยุ่งยากไปเองหรือเปล่า จะตรวจสอบทำไมทุก key
ตรวจสอบเอาตอน onchange ทีเดียวเลยดีกว่าไหม
และการอ้างอิง หลาย record การส่งค่า เข้า function ด้วย event object คุณรู้จัก event object มากขนาดไหน
มันมีประโยชน์มาก ถ้าคุณจะเรียนรู้ว่ามันมี property method อะไรให้ใช้อีกบ้าง เปิดหา document อ่านเลยครับ
เริ่มต้นให้หน่อย event.target คืออะไร
ศึกษาให้ดีจะทำให้คุณใช้งาน มันได้อีกเยอะ
|
 |
 |
 |
 |
Date :
2018-04-17 16:43:51 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมใส่ while สามารถทำงานได้มี alert แล้วครับ แต่ว่า ใส่เลขอะไร ก็โชว์มาหมด เลย ไม่ใช่ ว่า มากกว่า 5000 ขึ้นนะครับ แนะนำด้วยครับ
Code (PHP)
<?php $test=5000;?>
<SCRIPT language=JavaScript>
function check_number(event)
{
var e=window.event?window.event:event;
var e_k=e.keyCode?e.keyCode:e.which?e.which:e.charCode;
if (!(e_k == 46 || e_k == 8 || e_k == 13 || (e_k>=96 && e_k<=105)||(e_k>=48 && e_k<=57)))
{
alert("ต้องเป็นตัวเลขเท่านั้น... \nกรุณาตรวจสอบข้อมูลของท่านอีกครั้ง...");
document.form2.money.value="";
document.form2.money.focus();
return false;
}
return true;
}
function check_number2()
{
var u_score=<?=$test?>;
/*while(document.form2.money.value > u_score);*/
while(document.form2.money > u_score);
u_score++
{
alert('คุณป้อนจำนวนเกินกว่าจำนวนที่คุณได้รับกรุณาป้อนใหม่ครับ');
document.form2.money.value="";
document.form2.money.focus();
return false;
}
}
</script>
|
 |
 |
 |
 |
Date :
2018-04-18 10:07:55 |
By :
owenback |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
while ( document.form2.money.value > u_score){
ที่ คอมเม้นท ไว้ถูกต้องแล้ว และเอา ; u_socre++ ออกไป
และแทนที่จะใช้ while ทำไม่ใช้ if ตรงๆ ง่ายกว่ากันเยอะ ชักงงกับ algorithm ของจขกท แล้วนะครับเนี่ย
เมื่อก่อนก็เห็นจะเข้าใจอะไรต่างๆ ได้ดี มาตอนหลังนี้ สงสัยต้องเปลี่ยนชื่อเป็น super difficult ซะแล้วมั้ง 55555
ปล.
document.form2.money.value="";
document.form2.money.focus();
ตรงนี้ใช้ e.target แทน document.form2.money ได้เลย
event.target จะชึ้ไปยัง object ที่ส่งค่าเข้ามายัง function ที่ถูกเรียก มีค่าเท่ากับ this ใน jquery
|
ประวัติการแก้ไข 2018-04-18 10:34:04
 |
 |
 |
 |
Date :
2018-04-18 10:30:30 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แก้ตามที่อาจารย์ว่าแล้วครับ ยังเหมือนเดิมเลยครับ 555+
ใส่เลข 8 ขึ้น Error ครับ

|
 |
 |
 |
 |
Date :
2018-04-18 11:58:36 |
By :
owenback |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
<td>
<div class="form-group col-xl-100">
<input type="text" name="money" id="textfield"
onkeyup="return check_number2(event);"
onkeydown="return check_number(event);"
class="form-control">s</div>
</td>
<SCRIPT language=JavaScript>
function check_number(e) {
var e_k=e.keyCode?e.keyCode:e.which?e.which:e.charCode;
if (!(e_k == 46 || e_k == 8 || e_k == 13 || (e_k>=96 && e_k<=105)||(e_k>=48 && e_k<=57)))
{
alert("ต้องเป็นตัวเลขเท่านั้น... \nกรุณาตรวจสอบข้อมูลของท่านอีกครั้ง...");
e.target.value="";
e.target.focus();
return false;
}
return true;
}
function check_number2(e){
var u_score=<?=$test?>;
var vl=e.target.value * 1; // แปลงเป็นตัวเลข
if( vl > u_score){
alert('คุณป้อนจำนวนเกินกว่าจำนวนที่คุณได้รับกรุณาป้อนใหม่ครับ');
e.target.value="";
e.target.focus();
return false;
}
return true;
}
</script>
ใช้ document.form.name ไม่ได้เพราะ มันเป็น array
name ถูกประกาศหลายครั้งใน form
แต่ให้ใช้ event.target แทน เพราะเป็นการอ้างอิง object ที่เรียกใช้ function
|
 |
 |
 |
 |
Date :
2018-04-18 14:02:12 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
สุดยอดเลยครับ ถามอีกนิดได้มั้ยครับ 
ถ้าต้อง การ เอา 5000 เปลี่ยน มา เป็นเอาข้อมูลมาจาก Database ต้องทำไงบ้างนะครับ เป็น ข้อมูล loop เรื่อยๆๆ นะครับ
|
 |
 |
 |
 |
Date :
2018-04-18 16:53:55 |
By :
owenback |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 04
|