我试图同时为text
标签和value
标签设置td
和input
。但是单击该按钮后,td标签将充满数据,而输入标签将消失。
什么是最佳解决方案?
function myfunction() {
$("#sel").val("Kannan");
$(".sel").html("Kannan");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table border="1">
<tr>
<th>name</th>
</tr>
<tr>
<td class="sel">
<input type="text" id="sel">
</td>
</tr>
</table>
<button onClick="myfunction();">click</button>
答案 0 :(得分:0)
您需要删除JS中不必要的行
function myfunction() {
$("#sel").val("Kannan");
//$(".sel").html("Kannan"); Remove this line.
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table border="1">
<tr>
<th>name</th>
</tr>
<tr>
<td class="sel">
<input type="text" id="sel">
</td>
</tr>
</table>
<button onClick="myfunction();">click</button>