gpt4 book ai didi

javascript - 按下了哪个提交按钮?

转载 作者:搜寻专家 更新时间:2023-10-31 23:12:02 24 4
gpt4 key购买 nike

在这个 jsfiddle 中

http://jsfiddle.net/littlesandra88/eGRRb/

我是否提交了自动生成的按钮。每个表格行都有一个唯一的 ID 号,如果需要,每个提交按钮也可以获得相同的唯一号。

问题

问题是有多个提交按钮,那么我怎么知道按下了哪个呢?

HTML

<form action="" method="post">
<table class="alerts tablesorter" id="accTable" cellspacing="0">
<thead>
<tr class="header">
<th class="activity-header"> A </th>
<th class="activity-header"> Signed </th>
<th class="activity-header"> </th>
</tr>
</thead>

<tbody>

<tr class="row" id="7249">
<td class="activity-data">7249</td>
<!-- tablesorter can't sort a column with check boxes out-of-the-box, so it needs something to sort on. That is why the span tag is here -->
<!-- a jquery script is watching the state of the checkbox, so when clicked the value in the span is updated -->
<td class="checkbox"> <span style="display:none;">0</span> <input name="signed" type="checkbox" > </td>
<td class="edit-column"> <input value="Save" type="submit" name="7249"></td>
</tr>

<tr class="row" id="61484">
<td class="activity-data">61484</td>
<td class="checkbox"> <span style="display:none;">1</span> <input name="signed" type="checkbox" checked > </td>
<td class="edit-column"> <input value="Save" type="submit" name="61484"></td>
</tr>

</tbody>
</table>
</form>

JavaScript

$(document).ready(function() {
$("#accTable").tablesorter();

// :checkbox stops from executing the event on the save button. Same as input[type=checkbox]
$('#accTable input:checkbox').click(function() {
// insert 1 or 0 depending of checkbox state in the tag before the input tag. In this case <span> is before <input>
// this is done so tablesorter have something to sort on, as it doesn't support checkbox sort out of the box.
var order = this.checked ? '1' : '0';
$(this).prev().html(order);

$(this).parents("table").trigger("update");
});
});

// sends the form content to server side, and stay on page
$('form').live('submit', function() {

alert('test');

// don't redirect
return false;
});

最佳答案

你可以使用delegate():

$('form').delegate('input:submit','click',
function(){
alert(this.name);
return false;
});

JS Fiddle demo .


编辑以解决来自 OP 的评论中的问题:

Would it be possible to display 0 or 1 as the state of the associated checkbox with this approach?

是的,这是可能的,尽管它比我想要的要冗长一点:

$('form').delegate('input:submit','click',
function(){
var idString = this.name;
var checkboxState = $('#' + idString).find('input:checkbox').is(':checked');

if (checkboxState == true){
alert('1');
}
else {
alert('0');
}
return false;
});

JS Fiddle demo .

关于javascript - 按下了哪个提交按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7442483/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com