我正在尝试同时为td
标签和input
标签设置text
和value
。但是单击按钮后,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>
你需要删除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>