gpt4 book ai didi

jquery - 如何使用jquery获取输入按钮的值?

转载 作者:行者123 更新时间:2023-12-03 22:00:34 24 4
gpt4 key购买 nike

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

<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 的值。我不知道如何获取该值。

这是我的js:

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

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

最佳答案

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

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

http://jsfiddle.net/3QDM5/

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

关于jquery - 如何使用jquery获取输入按钮的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9721178/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com