get value for a input button using jquery

2019-06-15 04:47发布

i need to retrieve 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> 

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

here is my 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".

thanks in advance.

标签: jquery get
3条回答
小情绪 Triste *
2楼-- · 2019-06-15 05:22

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!

查看更多
Summer. ? 凉城
3楼-- · 2019-06-15 05:28

example :

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

where click(this.value) is function on javascript

function click(x)
{
    //x is value from onclick button
}
查看更多
聊天终结者
4楼-- · 2019-06-15 05:35

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

$(document).ready(function() { 
    $('#button1').click(function() { 
        var text = $(this).attr('value'); 
        $("#input").val(text); 
    }); 
}); 
查看更多
登录 后发表回答