javascript - 通过 id 更改表内的值
问题描述
我正在创建一个通过 id 应用程序使用 jquery 更改的表值,我不太了解 jquery 如何从使用 jquery 的输入中通过 id 更改表行
注意:我从另一个页面测试了以下代码,但代码是这样的
`<script>
$('#dicerik').html(icerik).find("#tablo2");
</script>`
我已经尝试过这样的事情,但我无法确定这改变其他行的真正方式
<script src="https://code.jquery.com/jquery-2.2.4.js" integrity="sha256-iT6Q9iMJYuQiMWNd9lDyBUStIq/8PuOW33aOqmvFpqI=" crossorigin="anonymous"></script>
<table>
<tr>
<th>İçerik</th>
<th>Tür</th>
<th>Dil</th>
</tr>
<tr>
<td>1</td>
<td>DENEXAMPLE</td>
<td>YineEXAMPLE</td>
</tr>
<tr>
<td>2</td>
<td>DENEXAMPLE2</td>
<td>YineEXAMPLE2</td>
</tr>
<tr>
<td>3</td>
<td>DENEXAMPLE3</td>
<td>YineEXAMPLE3</td>
</tr>
<tr>
<td>4</td>
<td>DENEXAMPLE4</td>
<td>YineEXAMPLE4</td>
</tr>
</table>
<input type="text" id="text1" name="text1" placeholder="id">
<input type="text" id="text2" name="text2" placeholder="text">
<button type="button" name="butonbu">Change it</button>
行示例 1 的第一个输入 id 及其第二个文本 YineEXAMPLE
预期结果是 id 1 行,第二个文本应该是“dene”
解决方案
尝试这个::
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-2.2.4.js" integrity="sha256-iT6Q9iMJYuQiMWNd9lDyBUStIq/8PuOW33aOqmvFpqI=" crossorigin="anonymous"></script>
<table>
<tr>
<th>İçerik</th>
<th>Tür</th>
<th>Dil</th>
</tr>
<tr>
<td>1</td>
<td>DENEXAMPLE</td>
<td>YineEXAMPLE</td>
</tr>
<tr>
<td>2</td>
<td>DENEXAMPLE2</td>
<td>YineEXAMPLE2</td>
</tr>
<tr>
<td>3</td>
<td>DENEXAMPLE3</td>
<td>YineEXAMPLE3</td>
</tr>
<tr>
<td>4</td>
<td>DENEXAMPLE4</td>
<td>YineEXAMPLE4</td>
</tr>
</table>
<input type="text" id="text1" name="text1" placeholder="id">
<input type="text" id="text2" name="text2" placeholder="text">
<button type="button" name="butonbu" onClick="changeIt()">Change it</button>
<script>
function changeIt()
{
$("table tr").each(function(index)
{
if (index != 0) {
$row = $(this);
var id = $row.find("td:first").text();
var searchID=$("#text1").val();
if(id==searchID)
{
$row.find("td:nth-child(2)").html($("#text2").val())
}
}
})
}
</script>
推荐阅读
- r - 将鼠标悬停在绘图输出上时不会出现减号
- c# - C# Parallel Foreach 没有按预期工作,它无异常退出
- javascript - 当第二次从第一个钩子中获取使用数据时,使用多个 React 钩子 useEffect 从 API 中获取数据
- r - 将文本文件导入 R 并更改其格式
- c - 无法解决 Visual Studio 2019 中未处理的异常
- go - Golang 的 base64 实现
- javascript - 使 Vueper-Slides 与 Gridsome 中的 Markdown 前端工作一起使用
- r - 边列表网络图 R
- python - 即使您关闭文件并重新启动计算机,变量也会存储其值?
- sql-server - 将 select 语句的结果作为参数传递给存储过程