当页面加载时我想让它调用一个为我做事的函数,但是JavaScript没有调用函数,我也不知道为什么。它没有给我任何错误,但它只是不会调用函数(),我怎么知道这是因为当我把document.write放在函数之前我可以看到"你好"印在我的页面上。这是代码:
<SCRIPT type="text/javascript">
window.onload = function() {
document.write("hello");
er=<? echo $res; ?>;
var res_name = <?php echo json_encode($res_name); ?>;
var res_des = <?php echo json_encode($res_des); ?>;
var res_price = <?php echo json_encode($res_price); ?>;
var errorno = <?php echo json_encode($errorno); ?>;
document.write(res_price);
if(errorno>0){
alert("Please enter a correct price for item number : "+errorno);
}
for(var p=1;p<=er+1;p++){
var table = document.getElementById("myTable");
var rowCount = table.rows.length;
// create a row element
var row = document.createElement("tr");
// add the row to the table
table.appendChild(row);
var colCount = table.rows[0].cells.length;
// get the select element
var dropdown = row.getElementsByTagName("form")[0];
// get the current total of dropdowns in the table
total = (table.getElementsByTagName("tr").length)-1;
if(p==er+1){
for(var i=0; i<colCount; i++) {
var newcell = row.insertCell(i);
//newcell.innerHTML = table.rows[1].cells[i].innerHTML;
if(i==0){
newcell.innerHTML =p;
}else if(i==1){
newcell.innerHTML ="<Input type='text' name='name"+p+"' size=30 />";
}else if(i==2){
newcell.innerHTML ="<Input type='text' name='des"+p+"' size=50 />";
}else if(i==3){
newcell.innerHTML ="<Input type='number' name='price"+p+"' />";
}else if(i==4){
newcell.innerHTML ="<Input type='submit' value='Save' />";
}
}
}
else{
for(var i=0; i<colCount; i++) {
var newcell = row.insertCell(i);
//newcell.innerHTML = table.rows[1].cells[i].innerHTML;
if(i==0){
newcell.innerHTML =p+".";
}else if(i==1){
newcell.innerHTML ="<Input type='text' name='name"+p+"' size=30 value='"+res_name[p]+"' />";
}else if(i==2){
newcell.innerHTML ="<Input type='text' name='des"+p+"' size=50 value='"+res_des[p]+"' />";
}else if(i==3){
newcell.innerHTML ="<Input type='number' name='price"+p+"' value='"+res_price[p]+"' />";
}else if(i==4){
newcell.innerHTML ="<Input type='button' name='delete"+p+"' value='Delete row' onClick='deleteRow(\"myTable\","+p+");' />";
}
}
}
document.getElementById('total').value = p;
}
};
function addRowEntry(tableID ){
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
// create a row element
var row = document.createElement("tr");
// add the row to the table
table.appendChild(row);
var colCount = table.rows[0].cells.length;
// get the select element
var dropdown = row.getElementsByTagName("form")[0];
// get the current total of dropdowns in the table
total = (table.getElementsByTagName("tr").length)-1;
for(var i=0; i<colCount; i++) {
var newcell = row.insertCell(i);
//newcell.innerHTML = table.rows[1].cells[i].innerHTML;
if(i==0){
newcell.innerHTML =total+".";
}else if(i==1){
newcell.innerHTML ="<Input type='text' name='name"+total+"' size=30 />";
}else if(i==2){
newcell.innerHTML ="<Input type='text' name='des"+total+"' size=50 />";
}else if(i==3){
newcell.innerHTML ="<Input type='number' name='price"+total+"' />";
}else if(i==4){
newcell.innerHTML ="<Input type='button' name='delete"+i+"' value='Delete row' onClick='deleteRow(\"myTable\","+i+");' />";
}
}
document.getElementById('total').value = total;
}
function deleteRow(tableID , po){
var table = document.getElementById(tableID);
table.deleteRow(po);
document.getElementById('total').value = (document.getElementById('total').value);
var count = (document.getElementById('total').value)-1 ;
for(var o=0 ; o<= count ; o++){
var x=document.getElementById(tableID).rows[o+1].cells;
x[0].innerHTML=o+1+".";
x[4].innerHTML="<Input type='button' name='delete"+o+"' value='Delete row' onClick='deleteRow(\"myTable\","+(o+1)+");' />";
}
}
function con(){
var conf = confirm("Are you sure you want to make the changes ?");
if(con){
document.getElementById('yn').value = "yes";
}else{
document.getElementById('yn').value = "no";
}
}
答案 0 :(得分:1)
您提供的代码是正确的。 见JSFiddle:
但是,可能有一些原因可能导致您的功能无法执行。
window.onload
函数,所以你的方法根本没有执行等
准确地说,并准确地检测问题,您需要提供有关该问题的更多信息。
答案 1 :(得分:0)
只需用下面的代码替换它的工作..
<script type="text/javascript">
window.onload = function () {
document.write("hello");
er = "";
var res_name = "";
var res_des = "";
var res_price = "";
var errorno = "";
document.write(res_price);
if (errorno > 0) {
alert("Please enter a correct price for item number : " + errorno);
}
for (var p = 1; p <= er + 1; p++) {
var table = document.getElementById("myTable");
var rowCount = table.rows.length;
// create a row element
var row = document.createElement("tr");
// add the row to the table
table.appendChild(row);
var colCount = table.rows[0].cells.length;
// get the select element
var dropdown = row.getElementsByTagName("form")[0];
// get the current total of dropdowns in the table
total = (table.getElementsByTagName("tr").length) - 1;
if (p == er + 1) {
for (var i = 0; i < colCount; i++) {
var newcell = row.insertCell(i);
//newcell.innerHTML = table.rows[1].cells[i].innerHTML;
if (i == 0) {
newcell.innerHTML = p;
} else if (i == 1) {
newcell.innerHTML = "<Input type='text' name='name" + p + "' size=30 />";
} else if (i == 2) {
newcell.innerHTML = "<Input type='text' name='des" + p + "' size=50 />";
} else if (i == 3) {
newcell.innerHTML = "<Input type='number' name='price" + p + "' />";
} else if (i == 4) {
newcell.innerHTML = "<Input type='submit' value='Save' />";
}
}
}
else {
for (var i = 0; i < colCount; i++) {
var newcell = row.insertCell(i);
//newcell.innerHTML = table.rows[1].cells[i].innerHTML;
if (i == 0) {
newcell.innerHTML = p + ".";
} else if (i == 1) {
newcell.innerHTML = "<Input type='text' name='name" + p + "' size=30 value='" + res_name[p] + "' />";
} else if (i == 2) {
newcell.innerHTML = "<Input type='text' name='des" + p + "' size=50 value='" + res_des[p] + "' />";
} else if (i == 3) {
newcell.innerHTML = "<Input type='number' name='price" + p + "' value='" + res_price[p] + "' />";
} else if (i == 4) {
newcell.innerHTML = "<Input type='button' name='delete" + p + "' value='Delete row' onClick='deleteRow(\"myTable\"," + p + ");' />";
}
}
}
document.getElementById('total').value = p;
}
};
function addRowEntry(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
// create a row element
var row = document.createElement("tr");
// add the row to the table
table.appendChild(row);
var colCount = table.rows[0].cells.length;
// get the select element
var dropdown = row.getElementsByTagName("form")[0];
// get the current total of dropdowns in the table
total = (table.getElementsByTagName("tr").length) - 1;
for (var i = 0; i < colCount; i++) {
var newcell = row.insertCell(i);
//newcell.innerHTML = table.rows[1].cells[i].innerHTML;
if (i == 0) {
newcell.innerHTML = total + ".";
} else if (i == 1) {
newcell.innerHTML = "<Input type='text' name='name" + total + "' size=30 />";
} else if (i == 2) {
newcell.innerHTML = "<Input type='text' name='des" + total + "' size=50 />";
} else if (i == 3) {
newcell.innerHTML = "<Input type='number' name='price" + total + "' />";
} else if (i == 4) {
newcell.innerHTML = "<Input type='button' name='delete" + i + "' value='Delete row' onClick='deleteRow(\"myTable\"," + i + ");' />";
}
}
document.getElementById('total').value = total;
}
function deleteRow(tableID, po) {
var table = document.getElementById(tableID);
table.deleteRow(po);
document.getElementById('total').value = (document.getElementById('total').value);
var count = (document.getElementById('total').value) - 1;
for (var o = 0 ; o <= count ; o++) {
var x = document.getElementById(tableID).rows[o + 1].cells;
x[0].innerHTML = o + 1 + ".";
x[4].innerHTML = "<Input type='button' name='delete" + o + "' value='Delete row' onClick='deleteRow(\"myTable\"," + (o + 1) + ");' />";
}
}
function con() {
var conf = confirm("Are you sure you want to make the changes ?");
if (con) {
document.getElementById('yn').value = "yes";
} else {
document.getElementById('yn').value = "no";
}
}
</script>
快乐编码.. :)