JQuery - 根据 name 属性调用 jquery 按钮点击事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9696604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 11:10:05  来源:igfitidea点击:

JQuery - Call the jquery button click event based on name property

jquery

提问by Manikandan Sethuraju

I have one HTML buttonlike,

我有一个HTML 按钮,例如,

<input type="button" id="btnSubmit" name="btnName" class="btnclass" value="Click Me" />

I want to call JQuery button clickevent based on idproperty means, we use the code like,

我想根据属性手段调用JQuery按钮点击事件id,我们使用这样的代码,

$("#btnSubmit").click(function(){ 
    ////////
});

as well as call button clickevent based on classproperty means, we use the code like,

以及基于属性手段调用按钮点击事件class,我们使用这样的代码,

$(".btnclass").click(function(){
    /////////
});

and My question is, I want to call the button clickevent based on nameproperty means, How to do this?

我的问题是,我想根据属性方法调用按钮单击事件name,该怎么做?

回答by defau1t

You can use the name property for that particular element. For example to set a border of 2px around an input element with name xyz, you can use;

您可以为该特定元素使用 name 属性。例如,要在名称为 xyz 的输入元素周围设置 2px 的边框,您可以使用;

$(function() {
    $("input[name = 'xyz']").css("border","2px solid red");
})

DEMO

演示

回答by Joseph

$('element[name="element_name"]').click(function(){
    //do stuff
});

in your case:

在你的情况下:

$('input[name="btnName"]').click(function(){
    //do stuff
});

回答by Joe Landsman

You can use normal CSS selectors to select an element by name using jquery. Like this:

您可以使用普通的 CSS 选择器使用 jquery 按名称选择元素。像这样:

Button Code
<button type="button" name="mybutton">Click Me!</button>

Selector & Event Bind Code
$("button[name='mybutton']").click(function() {});

回答by Baz1nga

You have to use the jquery attribute selector. You can read more here:

您必须使用 jquery 属性选择器。你可以在这里阅读更多:

http://api.jquery.com/attribute-equals-selector/

http://api.jquery.com/attribute-equals-selector/

In your case it should be:

在你的情况下,它应该是:

$('input[name="btnName"]')