javascript - 如何使 javascript 代码与按钮一起使用以重新排序表行?
问题描述
下面的代码执行表中行的定位。
javascript中的以下代码与输入类型=“按钮”完美配合
<input type = "button" value = "move up" class = "move up" />
如何使下面的javascript函数与下面的按钮一起使用?
<button type = "submit" class = "btn btn-light" value = "move up">
<span class = "glyphicon glyphicon-hand-up"> </ span>
</button>
我在下面使用的Javascript代码
$('#mytable input.move').click(function() {
var row = $(this).closest('tr');
if ($(this).hasClass('up'))
row.prev().before(row);
else
row.next().after(row);
});
代码 HTML
<table class="table" id="mytable">
<tr>
<td>row 1</td>
<td>
<input type="button" value="move up" class="move up" />
<button type="submit" class="btn btn-light" value="move up">
<span class="glyphicon glyphicon-hand-up"></span>
</button>
</td>
<td>
<input type="button" value="move down" class="move down" />
<button type="submit" class="btn btn-light" value="move down">
<span class="glyphicon glyphicon-hand-down"></span>
</button>
</td>
</tr>
<tr>
<td>row 2</td>
<td>
<input type="button" value="move up" class="move up" />
<button type="submit" class="btn btn-light" value="move up">
<span class="glyphicon glyphicon-hand-up"></span>
</button>
</td>
<td>
<input type="button" value="move down" class="move down" />
<button type="submit" class="btn btn-light" value="move down">
<span class="glyphicon glyphicon-hand-down"></span>
</button>
</td>
</tr>
</table>
解决方案
要使新标记使用 abutton
来执行与 相同的input
操作,请将脚本更改为此
$('#mytable button.move').click(function() {
var row = $(this).closest('tr');
if ($(this).hasClass('up'))
row.prev().before(row);
else
row.next().after(row);
});
然后添加move up
到button
s 类
<button type = "submit" class = "btn btn-light move up" value = "move up">
<span class = "glyphicon glyphicon-hand-up"> </ span>
</ button>
如果您无法将类添加到button
,请使用此脚本并评估button
的value属性,以确定是“向上”还是“向下”。
$('#mytable button[value="move up"]').click(function() {
var row = $(this).closest('tr');
if ($(this).val().contains('up'))
row.prev().before(row);
else
row.next().after(row);
});
请注意,在上面我使用属性选择器button[value="move up"]
来定位button
. 当然也可以使用它现有的类之一。
推荐阅读
- php - How I compare a string of a file, and if it does not exist, I write it in the file in PHP?
- azure - 通过 Azure 从 c# 后端推送通知是使用 GCM 而不是 FCM 发送的
- ios - libRNLaunchNavigator 的 React 本机链接器命令失败
- laravel - Voyager 管理面板链接已损坏
- php - 如何通过索引 $sheet->getRowIterator() 获取行?
- bash - 将“tail -f”输出重定向到 COPY 命令
- html - 背景颜色拒绝在 Outlook 中应用
- javascript - Html 未按预期呈现 javascript 断线
- java - 交换可绘制图像的文本
- db2 - 如何根据一定的逻辑创建一个检索年份的函数