09/10/2018, 23:53 
               
            đổi chử thành số trong javascript
               
					trong html mình khai báo một text và hàm onclick của button như sau:
<input type="text" id="somon"/>
<input type="button" value="Phát sinh" onclick='bangdiem(somon)>
function bangdiem(somon)
{
var i;
document.write("<table>");
document.write("<tr>");
for (i=1;i<=parseInt(somon.value);i++)
document.write("<td>" + "môn" + i + "</td>");
document.write("</tr>");
document.write("</table>");
}
cho mình hỏi là tại sao vòng lặp for của mình lại không chạy mặc dù mình đã đổi kiểu text thành số bằng cách dùng hàm parseInt(mình đã thử document.write(parseInt(somon.value)) thì thấy vẫn đúng là hiện ra giá trị của mình) help me .thank
            
         <input type="text" id="somon"/>
<input type="button" value="Phát sinh" onclick='bangdiem(somon)>
function bangdiem(somon)
{
var i;
document.write("<table>");
document.write("<tr>");
for (i=1;i<=parseInt(somon.value);i++)
document.write("<td>" + "môn" + i + "</td>");
document.write("</tr>");
document.write("</table>");
}
cho mình hỏi là tại sao vòng lặp for của mình lại không chạy mặc dù mình đã đổi kiểu text thành số bằng cách dùng hàm parseInt(mình đã thử document.write(parseInt(somon.value)) thì thấy vẫn đúng là hiện ra giá trị của mình) help me .thank
            Bài liên quan
         
               
            




thiếu dấu '
Làm như vầy thì chạy nè
<html> <body> <script type="text/javascript"> function bangdiem(somon) { var i; document.write("<table>"); document.write("<tr>"); for (i=1;i<=parseInt(somon.value);i++) document.write("<td>" + "môn" + i + "</td>"); document.write("</tr>"); document.write("</table>"); } </script> <input type="text" id="somon"/> <input type="button" value="Phát sinh" onclick='bangdiem(somon)'/> </body> </html>j=somon.value là ok
j=somon.value là ok
Còn j=somon.value cũng tương tự
<html> <head> </head> <body> <input type="text" id="somon"/> <input type="button" value="Phát sinh" onclick='bangdiem(somon.value)'/> </body> <script type="text/javascript"> function bangdiem(somon) { var i; document.write("<table>"); document.write("<tr>"); for (i=1;i<=somon;i++) document.write("<td>" + "môn" + i + "</td>"); document.write("</tr>"); document.write("</table>"); } </script> </html>Sửa lại nhé
<html>
<head>
<META http-equiv=Content-Type content="text/html; charset=UTF-8">
</head>
<body>
<input type="text" id="somon"/>
<input type="button" value="Phát sinh" onclick='bangdiem(somon.value)'/>
</body>
<script type="text/javascript">
function bangdiem(somon)
{
n = parseInt(document.getElementById('somon').value);
if(isNaN(n))
alert("Nhập số vào ông nội!! ^__^");
document.write("<table>");
document.write("<tr>");
for (i=1;i<=n;i++)
document.write("<td>" + "môn" + i + "</td>");
document.write("</tr>");
document.write("</table>");
}
</script>
</html>
Thực ra nếu làm như bạn thì 0 cần truyền tham số chi nữa! Cái getElementById là làm theo chuẩn W3C thì phải? Làm cái này thì độ tương thích cao hơn.
Mình phải gán vô biến tất cả các HTML cần gán, sau đó gán 1 lần vô innerHTML thì mới được. Còn làm từng khúc thì 0 được
<html> <head> <META http-equiv=Content-Type content="text/html; charset=UTF-8"> </head> <body> <input type="text" id="somon"/> <input type="button" value="tạo bảng" onclick='bangdiem()'/> <div id="dong" name="dong"> Đây là khu vực DIV </div> </body> <script type="text/javascript"> function bangdiem() { n = parseInt(document.getElementById('somon').value); d=document.getElementById('dong'); d.innerHTML=d.innerHTML+ "<b> hihihih</b>"; a="<table id='hihi' name='hihi' border='2' >"; a=a+"<tr>"; for (i=1;i<=n;i++) a=a+ "<td>" + "môn " +i+ " </td>"+"\n"; a=a+"</tr>"; a=a+"</table>"; d.innerHTML=d.innerHTML+ a; } </script> </html>Làm như vầy thì 0 được nè!
<html> <head> <META http-equiv=Content-Type content="text/html; charset=UTF-8"> </head> <body> <input type="text" id="somon"/> <input type="button" value="tạo bảng" onclick='bangdiem()'/> <div id="dong" name="dong"> Đây là khu vực DIV </div> </body> <script type="text/javascript"> function bangdiem() { n = parseInt(document.getElementById('somon').value); d=document.getElementById('dong'); d.innerHTML=d.innerHTML+ "<b> hihihih</b>"; d.innerHTML=d.innerHTML+="<table id='hihi' name='hihi' border='2' >"; d.innerHTML=d.innerHTML+"<tr>"; for (i=1;i<=n;i++) d.innerHTML=d.innerHTML+ "<td>" + "môn " +i+ " </td>"+"\n"; d.innerHTML=d.innerHTML+"</tr>"; d.innerHTML=d.innerHTML+"</table>"; } </script> </html>var markup = element.innerHTML;
element.innerHTML = markup;