使用 jquery 获取输入按钮的值

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

get value for a input button using jquery

jqueryget

提问by ssss05

i need to retrieve value for a clickable button using jquery. i have 3 buttons under a <div>.

我需要使用 jquery 检索可点击按钮的值。我在 a 下有 3 个按钮<div>

<div class="button1">
        <input type="button" value="one" id="One" onclick= "location.href='index.html'"/> 
        <input type="button" value="two" id="Two" onclick="location.href='index.html'" />
        <input type="button" value="three" id="Three" onclick="location.href='index.html'" />
 </div> 

if i click on button 1 i need to get value for a button 1. i dunno how exactly get the value of this.

如果我单击按钮 1,我需要获取按钮 1 的值。我不知道如何准确获取此值。

here is my js.

这是我的js。

$(document).ready(function() {
    $('.button1').click(function() {
        var text = $(this).text();
        $("input").val(text);
        });
});

i know this js script i wrote as wrong. because am getting values as "one,two,three".

我知道我写的这个 js 脚本是错误的。因为我得到的值是“一、二、三”。

thanks in advance.

提前致谢。

回答by Richard Dalton

Like any other input, to get the value of a button use .val():

与任何其他输入一样,要获取按钮的值,请使用 .val():

$('input:button').click(function() {
    alert($(this).val());
});?

http://jsfiddle.net/3QDM5/

http://jsfiddle.net/3QDM5/

I'm not sure exactly what you're trying to do with your script though!

我不确定你到底想用你的脚本做什么!

回答by Shane

use attr http://api.jquery.com/attr/

使用 attr http://api.jquery.com/attr/

$(document).ready(function() { 
    $('#button1').click(function() { 
        var text = $(this).attr('value'); 
        $("#input").val(text); 
    }); 
}); 

回答by Varanka

example :

例子 :

<input type="button" value="one" id="One" onclick= "click(this.value)'"/>

where click(this.value)is function on javascript

其中click(this.value)是 javascript 上的函数

function click(x)
{
    //x is value from onclick button
}