只有第一個給定的值被插入表中。變量i用於遞增表中tr的索引值,它是遞增的,但值不插入。其他人不添加。無法弄清楚原因。我是jquery的新手。使用jquery中的表格向表中插入值
<!DOCTYPE html>
<html>
<head>
<title>My first Page</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
var i=0;
$(document).ready(function(){
$("#hello").click(function(){
var a=$("<td></td>").text($("#test1").val());
var b=$("<td></td>").text($("#test2").val());
var c=$("<td></td>").text($("#test3").val());
var d=$("<td></td>").text($("#test4").val());
var e=$("<td></td>").text($("#test5").val());
$($("table tr").eq(i)).after(e).after(d).after(c).after(b).after(a);
i=i+1;
});
});
</script>
</head>
<body>
<div>
<h1>Hello Talib</h1>
<div>IT</div>
<div>WILL</div>
<div>DISAPPEAR</div>
</div>
First Name: <input type="text" value="fname" id ="test1"><br>
Last Name: <input type="text" value="lname" id="test2"><br>
Roll Number: <input type="text" value="rnum" id="test3"><br>
Faculty:
<select name="Faculty" id="test4">
<option value="CE">Computer Engineering</option>
<option value="BE">Biomedical Engineering</option>
<option value="CV">Civil Engineering</option>
<option value="TE">Telecommunication Engineering</option>
</select>
<br>
Semester:
<input type="radio" value="1st" name="sem" id="test5">1st
<input type="radio" value="2nd" name="sem" id="test5">2nd
<input type="radio" value="3rd" name="sem" id="test5">3rd
<input type="radio" value="4th" name="sem" id="test5">4th
<input type="radio" value="5th" name="sem" id="test5">5th
<input type="radio" value="6th" name="sem" id="test5">6th
<input type="radio" value="7th" name="sem" id="test5">7th
<input type="radio" value="8th" name="sem" id="test5">8th
<br>
<input type="submit" value="Submit"id="hello">
<table border="1" style="border: solid 1px black">
<tr>
<td>First Name</td>
<td>Last Name</td>
<td>Roll Number</td>
<td>Faculty</td>
<td>Semester</td>
</tr>
</table>
</body>
</html>
非常感謝.. –
@ user2868650沒問題。不要忘記上傳並接受答案。 –