查找点击按钮的ID [英] Find Id of clicked button

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

问题描述

我想获取单击按钮的ID,因为我的表单上有4-5个按钮.

I wanted to get the id of clicked button since i have 4-5 buttons on my form.

    <button type="submit" style="height: 30px" id="btnHelp" name="btnHelp" onclick="ShowHelp(2);return false;">Help</button>

    <button type="button" style="height: 30px" id="btnClose" name="btnClose" onclick="Close();return false;">Close</button>

    <button type="button" style="height: 30px" id="btnSave" name="btnSave" onclick="Save();return false;">Close</button>

...............................

无论单击哪个按钮,我只想获取该按钮的ID.

Whichever may be the button click, I just want to get id of that button.

$(function () {

        var id = $(this).attr('id');

        alert(id);
})

$("input").click(function (event) {
        var urlid = $(this).attr('id')
        alert(urlid);
    })

但是我收到的警报未定义.

but i am getting the alert as undefined.

我如何获得单击按钮的ID?

How can i get id of button clicked?

请帮助我.

推荐答案

尝试

:按钮选择器

选择所有按钮元素和按钮类型的元素.

Selects all button elements and elements of type button.

$(":button").click(function (event) {
    var urlid = this.id;
    alert(urlid);
});


小提琴演示


问题

$("input")->选择带有标签input的元素,例如. <input type="text"/>而不是<button>标记.

$("input") --> selects elements with tag input eg. <input type="text"/> but not <button> tag .

这篇关于查找点击按钮的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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