2017-07-19 19 views
0

enter image description here我是jquery的新手,javascript.please幫我解決了這個錯誤。當點擊複選框(超過3次)時,它會多次追加單元格。提前致謝。請幫幫我。 *它的附加細胞不止一次在jquery中添加行時複製

function suma() 
 
{ 
 
alert("hi"); 
 
$('#one').on("click", function(){ 
 
     $('#one input:checked').parent().parent().appendTo("#two"); 
 
     $("#two tr").append("<td> a </td>","<td> b</td>","<td> c</td>"); 
 
}); 
 
}
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script> 
 
</head> 
 
<body> 
 

 
<input type="submit" value="submit" onclick="suma()"/> 
 

 
<table id="one" border="1"> 
 
<tr> 
 
<td> <input type="checkbox"></td> 
 
<td>1</td> 
 
<td> 2</td> 
 
</tr> 
 
<tr> 
 
<td> <input type="checkbox"></td> 
 
<td> 3</td> 
 
<td> 4</td> 
 
</tr> 
 
<tr> 
 
<td> <input type="checkbox"></td> 
 
<td> 5</td> 
 
<td> 6</td> 
 
</tr> 
 
<tr> 
 
<td> <input type="checkbox"></td> 
 
<td>7</td> 
 
<td> 8</td> 
 
</tr> 
 
<tr> 
 
<td> <input type="checkbox"></td> 
 
<td>9</td> 
 
<td>10</td> 
 
</tr> 
 
<tr> 
 
<td> <input type="checkbox"></td> 
 
<td>11</td> 
 
<td>12</td> 
 
</tr> 
 

 
</table> 
 
<table id="two" border="1"> 
 
<th> msg1</th> 
 
<th> msg2</th> 
 
<th> msg3</th> 
 
<th> msg4</th> 
 
<th> msg5</th> 
 
<th> msg6</th> 
 
</table> 
 
</body> 
 
</html>

+2

爲什麼您使用jQuery 1.3.1有什麼特別的原因嗎? – Gerard

+0

這是一個工作的例子嗎?你發佈的那個。? – incorelabs

+0

清楚地告訴我們你在找什麼或期待的答案? – lalithkumar

回答

0

試試這個,

function suma() { 
    $('#one').on("click", function() { 
    var appendStr = "<td> a </td><td> b</td><td> c</td>"; 
    $('#one input:checked').parent().parent().wrap("<tr></tr>") 
             .append(appendStr).appendTo("#two"); 
    // add string inside TR and append the a b c then appendTo table. 
    }); 
} 

工作example

希望幫助,

+0

非常感謝你@Berkay – Raghavi

0

好吧,我不知道爲什麼你的內部功能的jQuery的單擊事件。

本替換你的函數:

function suma(){ 
    $('#one input:checked').parent().parent().appendTo("#two"); 
    $("#two tr").append("<td> a </td>","<td> b</td>","<td> c</td>"); 
} 
0

檢查股利(#two)已經不具備表(「#一」),然後用查找功能添加它然後檢查表乾沒有這個tr然後添加它是你添加表的依賴,如果這將導致多個具有相同的id,所以依賴於類更喜歡。

function suma() 
{ 
alert("hi"); 
$('#one').on("click", function(){ 
if($("#two").find("#one")==0){ 
    $('#one input:checked').parent().parent().appendTo("#two"); 
} 
if($("#two").find("#one").find("<tr><td> a </td>","<td> b</td>","<td> c</td></tr>") ==0){ 
    $("#two").find("#one").append("<tr><td> a </td>","<td> b</td>","<td> c</td></tr>"); 
} 
}); 
} 
0

您需要添加額外的文本,然後需要後追加到#two
嘗試下面的代碼,

function suma() 
 
{ 
 

 
var row = $('#one input:checked').parent().parent().append("<td> a </td><td> b</td><td> c</td>"); 
 
    
 
row.appendTo("#two"); 
 
     
 

 
}
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script> 
 
</head> 
 
<body> 
 

 
<input type="submit" value="submit" onclick="suma()"/> 
 

 
<table id="one" border="1"> 
 
<tr> 
 
<td> <input type="checkbox"></td> 
 
<td>1</td> 
 
<td> 2</td> 
 
</tr> 
 
<tr> 
 
<td> <input type="checkbox"></td> 
 
<td> 3</td> 
 
<td> 4</td> 
 
</tr> 
 
<tr> 
 
<td> <input type="checkbox"></td> 
 
<td> 5</td> 
 
<td> 6</td> 
 
</tr> 
 
<tr> 
 
<td> <input type="checkbox"></td> 
 
<td>7</td> 
 
<td> 8</td> 
 
</tr> 
 
<tr> 
 
<td> <input type="checkbox"></td> 
 
<td>9</td> 
 
<td>10</td> 
 
</tr> 
 
<tr> 
 
<td> <input type="checkbox"></td> 
 
<td>11</td> 
 
<td>12</td> 
 
</tr> 
 

 
</table> 
 
<table id="two" border="1"> 
 
<th> msg1</th> 
 
<th> msg2</th> 
 
<th> msg3</th> 
 
<th> msg4</th> 
 
<th> msg5</th> 
 
<th> msg6</th> 
 
</table> 
 
</body> 
 
</html>

0

我認爲你正在尋找這一點。

$('#one').on("click", function(){ 
 
     $('#one input:checked').parent().parent().appendTo("#two"); 
 
     $("#two tr").append("<td> a </td>","<td> b</td>","<td> c</td>"); 
 
});
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
</head> 
 
<body> 
 

 
<input type="submit" value="submit" onclick="suma()"/> 
 

 
<table id="one" border="1"> 
 
<tr> 
 
<td> <input type="checkbox"></td> 
 
<td>1</td> 
 
<td> 2</td> 
 
</tr> 
 
<tr> 
 
<td> <input type="checkbox"></td> 
 
<td> 3</td> 
 
<td> 4</td> 
 
</tr> 
 
<tr> 
 
<td> <input type="checkbox"></td> 
 
<td> 5</td> 
 
<td> 6</td> 
 
</tr> 
 
<tr> 
 
<td> <input type="checkbox"></td> 
 
<td>7</td> 
 
<td> 8</td> 
 
</tr> 
 
<tr> 
 
<td> <input type="checkbox"></td> 
 
<td>9</td> 
 
<td>10</td> 
 
</tr> 
 
<tr> 
 
<td> <input type="checkbox"></td> 
 
<td>11</td> 
 
<td>12</td> 
 
</tr> 
 

 
</table> 
 
<table id="two" border="1"> 
 
<th> msg1</th> 
 
<th> msg2</th> 
 
<th> msg3</th> 
 
    
 
</table> 
 
</body> 
 
</html>

+0

@Raghavi See根據你的圖片它的作品只需將你的jquery 1.3.1設置爲Jquery 2.1.1 –