我正在嘗試復制表格的一行并向其附加一個單選按鈕。我在 html 中宣告了 3 行,在 jquery 中宣告了一個添加按鈕,可以將名稱添加到表中。所有這些行都附加了一個洗掉按鈕。當我單擊開始按鈕時,我希望名稱轉到下一個沒有洗掉按鈕的 div 并在其上附加一個單選按鈕。并且用戶應該能夠對名稱進行投票。當我添加名稱時,會將一個沒有名稱值的額外單選按鈕傳遞給 div。我不明白為什么。我正在嘗試使用 jquery 制作一個投票系統,而不使用任何資料庫(臨時)。請幫忙。我不明白為什么額外的單選按鈕會被通過。只有當我使用添加按鈕添加候選人時它才會通過。
這是我的代碼:
$(document).ready(function()
{
$('#start').click(function()
{
$("#div1").hide();
$("#buttonset1").hide();
$("#div2").show();
$("#voterName").val("")
if( $('#candidateTable thead tr').children().length = 1)
{
if( $('#newTable thead').children().length == 0 )
{
$('#newTable thead').append('<tr></tr>');
}
$('#newTable thead tr').append('<th>' $($('#candidateTable thead tr').children()).text() '</th>');
$('#candidateTable tbody tr').each(function(i)
{
if( $('#newTable tbody').children().length != $('#candidateTable tbody').children().length)
{
$('#newTable tbody').append('<tr></tr>');
}
$('#newTable tbody tr:nth-child(' (i 1) ')').append('<td>' $($(this).children()[0]).text() '</td>' '<td><input type="radio" ></td>');
});
}
});
//delete candiates
$("#candidateTable").on('click', '.del', function()
{
$(this).closest('tr').remove();
});
//add candidate
$("#add").click(function()
{
//adding candidates
var candidatename = $("#candidatename").val();
if(candidatename.length!=0)
{
$("#candidateTable tbody").append('<tr><td>' candidatename '</td>' '<td>' '<button id="Delete">Delete</button>' '</td><tr>');
$("#candidatename").val('');
}
});
});
$(document).ready(function()
{
$("#final").click(function(){
$("#div2").hide();
$("#div4").show();
});
});
table, tr, th, td{
border: 1px solid #000;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1> Voting System </h1>
<body>
<div id="div1">
<table id="candidateTable">
<thead>
<tr>
<th id="candidatelist">Candidate Name</th>
</tr>
</thead>
<tbody>
<tr>
<td>XXXX</td>
<td><button class="del" id="Delete">Delete</button></td>
</tr>
<tr>
<td>YYYY</td>
<td><button class="del" id="Delete">Delete</button></td>
</tr>
<tr>
<td>ZZZZ</td>
<td><button class="del" id="Delete">Delete</button></td>
</tr>
</tbody>
</table>
</div>
<div id="buttonset1">
<br>
<br>
<input type="text" id="candidatename">
<button id="add">Add</button>
<br>
<br>
<button id="start">Start</button>
<br>
<br>
</div>
<br>
<form action="javascript:void(0)">
<div id="div2" style="display: none;">
<div id="buttonset2">
<label>Name</label>
<input type="text" name="Name" id="votername">
<button id="vote">Vote</button>
<br>
<br>
<table id="newTable">
<thead>
</thead>
<tbody>
<tr>
</tr>
</tbody>
</table>
</form>
<br>
<br>
<button id="final">Submit Votes</button>
</div>
</div>
<br>
<br>
<br>
<div id="div4" style="display:none;">
<h2>
Result
</h2>
</div>
</body>
</html>
uj5u.com熱心網友回復:
更改這行代碼:
$("#candidateTable tbody").append('<tr><td>' candidatename '</td>' '<td>' '<button class="del" id="Delete">Delete</button>' '</td><tr>');
有了這個:
$("#candidateTable tbody").append('<tr><td>' candidatename '</td><td><button class="del" id="Delete">Delete</button></td></tr>');
在您的添加功能中。現在它不會添加額外的一行。
完整的作業示例鏈接如下
在此處輸入鏈接描述
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/399143.html
標籤:javascript html 查询 css
上一篇:如何淡入/淡出文本 影像
