Ara*_*yan 4 html javascript jsp button
在我的页面中,我有许多编辑按钮,每个名称以"编辑"开头,然后是一些ID.我想知道我的任何编辑按钮是否被点击.详细说明,我有表格.在表单中,我有许多编辑按钮,名称以"编辑"开头,删除名称以"删除"开头的按钮和1个添加按钮.所有都是提交按钮.form onsubmit如果按钮是编辑确认("一些文本"),则调用我想要的JavaScript函数,否则提交表单.我怎么能用JavaScript做到这一点?我认为给所有这些按钮相同的id然后getElementById,但那么我如何改变?
使用jQuery很简单:
$(':button').click(function() {
// reference clicked button via: $(this)
var buttonElementId = $(this).attr('id');
});
Run Code Online (Sandbox Code Playgroud)
尝试一下:http: //jsfiddle.net/7YEay/
根据评论中的反馈进行更新
这是未经测试的/伪代码:
$(':submit').click(function(event) {
var buttonName = $(this).attr('name');
if (buttonName.indexOf('edit') >= 0) {
//confirm("some text") logic...
}
event.preventDefault();
});
Run Code Online (Sandbox Code Playgroud)
此文档也可能有用:http://api.jquery.com/submit/
| 归档时间: |
|
| 查看次数: |
9065 次 |
| 最近记录: |