使用jQuery同时为td标签和输入标签设置文本和值

时间:2019-03-22 13:43:04

标签: jquery html

我试图同时为text标签和value标签设置tdinput。但是单击该按钮后,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>

1 个答案:

答案 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>