哪个提交按钮被按下? [英] Which submit button was pressed?

查看:97
本文介绍了哪个提交按钮被按下?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个jsfiddle中



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



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



问题



问题是有多个提交按钮,所以如何知道哪些按钮被按下?

HTML

 < form action =method =post> 
< table class =alerts tablesorterid =accTablecellspacing =0>
< thead>
< tr class =header>
< / tr>
< / thead>

< tbody>

< tr class =rowid =7249>
< td class =activity-data> 7249< / td>
<! - tablesorter无法使用复选框对列进行排序,因此需要进行排序。这就是span标签在这里的原因 - >
<! - jquery脚本正在观察复选框的状态,因此单击时跨度中的值更新 - >
< td class =checkbox> < span style =display:none;> 0< / span> < input name =signedtype =checkbox> < / TD>
< td class =edit-column> < input value =Savetype =submitname =7249>< / td>
< / tr>

< tr class =rowid =61484>
< td class =activity-data> 61484< / td>
< td class =checkbox> < span style =display:none;> 1< / span> < input name =signedtype =checkboxchecked> < / TD>
< td class =edit-column> < input value =Savetype =submitname =61484>< / td>
< / tr>

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

JavaScript
< $ p $ $(document).ready(function(){
$(#accTable)。tablesorter();

:checkbox与输入相同[type = checkbox]
$('#accTable input:checkbox')。click(function(){
// insert 1 or 0 depends在这种情况下< span>在< input>
之前//这样做是为了使tablesorter有一些东西可以排序,因为它不支持复选框排序
var order = this.checked?'1':'0';
$(this).prev()。html(order);

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

//将表单内容发送到服务器端并保留在页面
$('form')。live('submit',function(){

alert('test');

//不重定向
返回false;
});


解决方案

您可以使用委托)

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

JS Fiddle演示






已编辑问题在OP中:

lockquote>

这种方法可以显示0或1作为关联复选框的状态吗?


是的,这是可能的,虽然它比我想要的更啰嗦一点:

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

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

JS Fiddle演示


In this jsfiddle

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

have I submit buttons that are auto-generated. Each table row gets an unique ID number, and if needed each submit button can get the same unique number as well.

Question

The problem is that there are multiple submit buttons, so how do I know which was pressed?

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;
});

解决方案

You could use delegate():

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

JS Fiddle demo.


Edited to address question in the comments, from OP:

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

Yeah, that's possible, though it's a little more long-winded than I'd like:

$('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.

这篇关于哪个提交按钮被按下?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆