2013-04-05 28 views
0

我有多個輸入類型複選框 - 如果其中任何一個被選中,我想追加一列(只是一次) - 即使其他複選框類型在第​​一個複選框類型後標記,我不想再次添加列,它一直在做。我沒有得到它每個複選框類型輸入只添加一次。不過,我也希望只在整個文檔中添加一列(即跨頁面上的所有複選框)。我想選擇複選框類型的輸入,然後附加表列

這裏的

<table id="edhist_table" > 
<tr id="table_headings"> 
    <th>Graduated?</th>  
</tr> 

    <tr>    
     <td><input type="checkbox" name="college_grad" value="yes">yes</input></td> 

</tr> 
<tr id='grad_part'> 

    <td><input type="checkbox" name="grad_grad1" value="yes">yes</input></td> 

</table> 


<script> 
//if any of the checkboxes are checked - shoudl append column 
$("[type=checkbox]").one("click",function() { 
    $("#table_headings").append("<th id='degree_heading'>Degree/Certificate Name</th>"); 

任何想法非常讚賞的代碼相關部分。

回答

1

.one最多會執行一個元素,這意味着它會爲每個元素觸發一次(這會導致您重複,您需要檢查元素是否已添加,如果不添加它,您可以更改它爲此:

$("[type=checkbox]").one("click",function() { 
    if ($("#table_headings #degree_heading").length==0) 
     $("#table_headings").append("<th id='degree_heading'>Degree/Certificate Name</th>"); 
}); 
+0

哦,我的天哪 - 謝謝你這使得完整意義上的 – user1769203 2013-04-05 19:38:04

+0

實際上還有其他的方法可以檢查,比如你不需要'length == 0'部分,但是我發現更明確的方式更容易理解。很高興它對你有效。 – lucuma 2013-04-05 20:49:02

1

只要閉上你的函數:

$("[type=checkbox]").one("click",function() { 
    $("#table_headings").append("<th id='degree_heading'>Degree/Certificate Name</th>"); 
}); // <-- Here 

演示:http://jsfiddle.net/pYx6b/

相關問題