如何使用jQuery获取输入按钮的值? [英] How to get value for a input button using jquery?

查看:42
本文介绍了如何使用jQuery获取输入按钮的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用jquery来获取可点击按钮的值.我在< div> 下有3个按钮:

I need to retrieve a value for a clickable button using jquery. I have 3 buttons under a <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> 

如果我单击按钮1,则需要获取按钮1的值.我不知道如何确切地获得此值.

If I click on button 1, I need to get value for a button 1. I don't know, how exactly get the value of this.

这是我的js:

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

我知道我写的这个脚本是错误的,因为我得到的值是"1、2、3".

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

推荐答案

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

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

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

http://jsfiddle.net/3QDM5/

我不确定您要使用脚本做什么!

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

这篇关于如何使用jQuery获取输入按钮的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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