gpt4 book ai didi

jquery - 使用 jQuery 检索按钮值

转载 作者:行者123 更新时间:2023-12-03 21:25:27 25 4
gpt4 key购买 nike

一个简单的,我试图使用 jQuery 检索按下按钮时的 value 属性,这就是我所拥有的:

<script type="text/javascript">
$(document).ready(function() {
$('.my_button').click(function() {
alert($(this).val());
});
});
</script>

<button class="my_button" name="buttonName" value="buttonValue">
Button Label</button>

在 Firefox 中,我的警报显示“buttonValue”,这很好,但在 IE7 中,它显示“按钮标签”。

我应该使用什么 jQuery 来始终获取按钮的值?或者我应该使用不同的方法?

非常感谢。

答案:我现在正在使用

<input class="my_button" type="image" src="whatever.png" value="buttonValue" />

最佳答案

由于按钮值是一个属性,因此您需要在 jquery 中使用 .attr() 方法。这应该可以做到

<script type="text/javascript">
$(document).ready(function() {
$('.my_button').click(function() {
alert($(this).attr("value"));
});
});
</script>

您还可以使用attr来设置属性,更多信息in the docs .

这仅适用于 JQuery 1.6+。有关旧版本,请参阅 postpostmodern 的答案。

关于jquery - 使用 jQuery 检索按钮值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/487056/

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