Javascript 获取点击按钮的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32842967/
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-23 14:16:32  来源:igfitidea点击:

Get value of the clicked button

javascriptjquery

提问by Zakaria Acharki

I have multiple buttons containing different values.

我有多个包含不同值的按钮。

My buttons :-

我的按钮:-

<button id="1" name="1" value="1">Button1</button>
<button id="2" name="2" value="2">Button2</button>

Now, if I click on Button1, I should get it's value. That is 1, and if I click Button2, I should get value 2. I have written this code :-

现在,如果我点击 Button1,我应该得到它的价值。那是1,如果我单击 Button2 ,我应该得到值2。我写了这段代码:-

<script type="text/javascript">
$("button").click(function() {
    var fired_button = $("button").val();
    alert(fired_button);
});
</script>

But it always alerts 1. What must I do to fix my code?

但它总是提醒1。我必须怎么做才能修复我的代码?

回答by Zakaria Acharki

UPDATED

更新

Use thisinstead of buttonin :

使用this代替buttonin :

var fired_button = $("button").val(); 

You have to use thisto target the current button clicked instead of buttonthat will select all buttons in the DOM, .val() makes it to get the value of the first button.

您必须使用this定位当前单击的按钮,而不是button选择 DOM 中的所有按钮, .val() 使其获取第一个按钮的值。



$("button").click(function() {
    var fired_button = $(this).val();
    alert(fired_button);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="1" name="1" value="1">Button1</button>
<button id="2" name="2" value="2">Button2</button>

回答by Christos

You could try something as simple as:

你可以尝试一些简单的事情:

$(this).val();

$(function(){
    $("button").click(function() {
        var fired_button = $(this).val();
        alert(fired_button);
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="1" name="1" value="1">Button1</button>
<button id="2" name="2" value="2">Button2</button>

Note: you should add your event listeners after the document is ready. This is why, I have enclosed the event handler in the

注意:您应该在文档准备好后添加事件侦听器。这就是为什么,我将事件处理程序包含在

$(function{})

This is a shorthand of

这是一个简写

$(document).ready(function(){})

For more information about this, please have a look here.

有关这方面的更多信息,请查看此处

回答by Akshay

You need to use thisvariable in order to access the clicked button's value.

您需要使用this变量才能访问单击按钮的值。

<script type="text/javascript">
$("button").click(function() {
    var fired_button = $(this).val();
    alert(fired_button);
});
</script>

This would return the valueof the button.

这将返回value按钮的 。

回答by freedomn-m

thiswill give you the element that was clicked, $(this)to get a jquery version.

this会给你被点击的元素,$(this)以获得一个 jquery 版本。

Update your code to:

将您的代码更新为:

$("button").click(function() {
    var fired_button = $(this).val();
    alert(fired_button);
});

回答by krishna

Try with $(this).val();. It will return clicked button value.

尝试使用$(this).val();. 它将返回单击的按钮值。

回答by Daniel Rosano

Use thisinside the click handler

使用this点击处理程序中

<script type="text/javascript">
$("button").click(function() {
    var fired_button = $(this).val();
    alert(fired_button);
});
</script>

回答by Akash Rao

Try $(this).val(). 'this' always refers to the current object.

试试$(this).val()。“this”总是指当前对象。

回答by Claeyssic

If you're using jQuery, you're looking for the .attr() function.

如果您正在使用 jQuery,那么您正在寻找 .attr() 函数。

$(this).attr("value")

That code will give you the value attribute of the html element designed by $(this) (or you precise the ID of the element).

该代码将为您提供由 $(this) 设计的 html 元素的 value 属性(或您精确的元素 ID)。