2013-11-25 128 views
0
var rowcount = 0; 

function addrow() { 
    rowcount++; 
    console.log("nis" + rowcount); 
    if (rowcount >= 2) { 

     return false; 
     rowcount = 0; 

    } 
    document.getElementById("myTableData").style.display = "block"; 
    var el = document.createElement('input'); 
    el.type = 'text'; 
    el.name = 'kname' + rowcount; 
    var hid = document.createElement('input'); 
    hid.type = 'hidden'; 
    hid.name = 'hid'; 
    hid.value = rowcount; 
    var del = document.createElement('input'); 
    del.type = 'button'; 
    del.name = 'delll'; 
    del.value = ''; 
    del.style.width = '30px'; 
    del.style.height = '26px'; 
    del.style.border = 'none'; 
    del.style.outline = 'none'; 
    del.style.background = 'url(./images/del-hover.png) no-repeat'; 
    del.onclick = function() { 
     tr.parentElement.removeChild(tr); 
     rowcount--; 
     if (rowcount == 0 || rowcount >= 2) { 
      document.getElementById("myTableData").style.display = "none"; 
      rowcount = 0; 
     } 
    }; 
    var el_r = document.createElement('input'); 
    el_r.type = 'radio'; 
    el_r.name = 'kgender' + rowcount; 
    el_r.value = 'female'; 
    el_r.defaultChecked = true; 
    var el_r2 = document.createElement('input'); 
    el_r2.type = 'radio'; 
    el_r2.name = 'kgender' + rowcount; 
    el_r2.value = 'male'; 
    var obj1 = document.createTextNode("Female"); 
    var obj2 = document.createTextNode("Male"); 
    var objLabel = document.createElement("label"); 
    objLabel.htmlFor = el_r.id; 
    objLabel.appendChild(el_r); 
    objLabel.appendChild(obj1); 
    var objLabel2 = document.createElement("label"); 
    objLabel2.htmlFor = el_r2.id; 
    objLabel2.appendChild(el_r2); 
    objLabel2.appendChild(obj2); 
    var el_s = document.createElement('select'); 
    el_s.name = "day1" + rowcount; 
    el_s.onchange = function() { 
     value_d = el_s.options[el_s.selectedIndex].value; 
    }; 
    for (var i = 1; i < 32; i++) { 
     var j = i; 
     j = document.createElement('option'); 
     j.text = i; 
     j.value = i; 
     el_s.appendChild(j); 
    } 
    var month = new Array("January", "Februray", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); 
    var el_sm = document.createElement('select'); 
    el_sm.name = "month1" + rowcount; 
    el_sm.onchange = function() { 
     var valuem = el_sm.options[el_sm.selectedIndex].value; 
    }; 
    for (var i = 0; i < month.length; i++) { 
     var j = i; 
     j = document.createElement('option'); 
     j.text = month[i]; 
     j.value = i + 1; 
     el_sm.appendChild(j); 
    } 
    var el_sy = document.createElement('select'); 
    el_sy.name = "year1" + rowcount; 
    el_sy.onchange = function() { 
     var valuey = el_sy.options[el_sy.selectedIndex].value; 

    }; 
    for (var i = 2013; i > 1950; i--) { 
     var j = i; 
     j = document.createElement('option'); 
     j.text = i; 
     j.value = i; 
     el_sy.appendChild(j); 
    } 
    var table = document.getElementById("myTableData"); 
    var tableBody = document.createElement('TBODY'); 
    table.appendChild(tableBody); 
    var tr = document.createElement('TR'); 
    tableBody.appendChild(tr); 
    var td = document.createElement('TD'); 
    td.width = '175'; 
    td.appendChild(el); 
    tr.appendChild(td); 
    var td = document.createElement('TD'); 
    td.width = '245'; 
    td.appendChild(objLabel); 
    td.appendChild(objLabel2); 
    tr.appendChild(td); 
    var td = document.createElement('TD'); 
    td.width = '245'; 
    td.appendChild(el_s); 
    td.appendChild(el_sm); 
    td.appendChild(el_sy); 
    tr.appendChild(td); 
    var td = document.createElement('TD'); 
    td.width = '20'; 
    td.appendChild(del); 
    tr.appendChild(td); 
    var td = document.createElement('TD'); 
    td.width = '10'; 
    td.appendChild(hid); 
    tr.appendChild(td); 
    myTableData.appendChild(table); 
} 

我的HTML的onclick不工作

<td colspan="4"> 
    <div id="myTableData" style="display:none;"> 
     <table> 
      <tr> 
       <td width="175">NAME</td> 
       <td width="245">&nbsp;Gender</td> 
       <td width="245">Date of Birth</td> 
      </tr> 
     </table> 
    </div> 
</td> 
<td colspan="2"> 
    <div class="addkidbg" onClick="addrow()" />ADDKID</div> 
</td> 

當我在addrow按鈕,點擊它告訴我三排,但我需要用戶點擊只有一次,如果用戶刪除行列名應該消失,但如果我點擊兩次addrow按鈕,然後單擊刪除按鈕.......否認其顯示列名稱。我想,如果用戶刪除該行的整體部分應b遠去又不能點擊超過一次

+0

我建議你使用HTML DOM方法添加/刪除表格內容。見下面: http://www.w3schools.com/jsref/met_table_insertrow.asp和http://www.w3schools.com/jsref/met_table_deleterow.asp – Billy

+0

但即時通訊工作在PHP語言 –

+0

呃..在你的問題..這絕對不是PHP .. – Billy

回答

0
function addrow() { 
    rowcount++; 
    console.log("nis" + rowcount); 
    if (rowcount >= 2) { 
     rowcount = 0; 
     return false; 
    } 
} 
<div class="addkidbg" onClick="return addrow();" />ADDKID</div> 

試試這個代碼更。你的函數返回一個值,所以在onclick事件中首先寫回報

+0

如果我點擊addkid兩次,然後我點擊刪除按鈕..........然後名稱,性別和出生日期行不會被刪除 –