我想在我的註冊頁面中創建動態表格,我放棄了使用插件,我只是要使用這個附加的東西。如果用戶選擇某個選項,我希望表格僅顯示。因此,這裏是我的html:JQuery追加表格
<div id="Reg_num2" style="display:none;" >
<button id="AddLine">Add Line</button>
<table border="1px" id="table">
<tr>
<td>First Name</td>
<td>Last Name</td>
<td>Phone</td>
<td>Email</td>
<td>Ethnicity</td>
</tr>
<tr>
<td><input type=text /></td>
<td><input type=text /></td>
<td><input type=text /></td>
<td><input type=text /></td>
<td><input type=text /></td>
</tr>
</table>
</div>
這是我的jQuery代碼:
$(document).ready(function(e) {
$("input[name= 'Reg_num_r']").change(function() {
if($(this).val()==1) {
$("#Reg_num2").hide();
} else {
$("#Reg_num2").show();
}
});
$("#table").on("click", "button", function() {
$(this).closest("tr").remove();
});
$("#AddLine").click(function() {
var row = "<tr><td><input type=text /></td><td><input type=text /></td><td><input type=text /></td><td> <input type=text /></td><td><input type=text /></td><td><button>X</button></td></tr>";
$("#table").append(row);
});
});
有兩個問題,我有:
- 當用戶選擇該表將不顯示適當的單選按鈕。代碼有什麼問題?
- 當我點擊添加按鈕時,它會提交整個頁面,我該如何解決這個問題?
在你的標記中沒有包含'Reg_num_r'名稱屬性和無線電輸入的元素,你能提供一個[jsfiddle'](http://jsfiddle.net/)嗎? – undefined
@Richard您是否嘗試過scape type = type ='text'的文本?並請按f12並告訴我們在選擇選項後出現控制檯錯誤! –