2012-09-16 108 views
0

我想在我的註冊頁面中創建動態表格,我放棄了使用插件,我只是要使用這個附加的東西。如果用戶選擇某個選項,我希望表格僅顯示。因此,這裏是我的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); 
    }); 
}); 

有兩個問題,我有:

  1. 當用戶選擇該表將不顯示適當的單選按鈕。代碼有什麼問題?
  2. 當我點擊添加按鈕時,它會提交整個頁面,我該如何解決這個問題?
+0

在你的標記中沒有包含'Reg_num_r'名稱屬性和無線電輸入的元素,你能提供一個[jsfiddle'](http://jsfiddle.net/)嗎? – undefined

+0

@Richard您是否嘗試過scape type = type ='text'的文本?並請按f12並告訴我們在選擇選項後出現控制檯錯誤! –

回答

1

「2)當我點擊添加按鈕,將其提交en輪胎頁面,而我該如何解決?「

與規定將type="submit"默認情況下沒有type屬性的<button>元素,所以如果它在一個表格,將提交表單。將其更改爲<button type="button">

我不知道你的單選按鈕的問題,雖然我傾向於使用.click而非.change - 請更新您的問題,以顯示相關的HTML。

1

問題是

if($(this).val()==1) { 

應該

if($(this).val()=="1") { 

第二的answere

$("#submitButton").submit(function(){ 
//do things 
return false; 
}); 
+0

'「1」== 1'將會是'true',不是嗎? (與「1」相比=== 1將是'false'。) – nnnnnn

+0

@AlbertoLeón試過了,它沒有奏效。奇怪的是,這個代碼在添加所有這個表格業務之前實際工作。我不明白添加表格代碼可能會發生什麼。 – Richard

+0

@nnnnnn val()總是返回一個字符串,所以1是一個int應該是false,不是true。 –