用jQuery检索Button值

一个简单的,我试图检索button的价值属性,当它使用jQuery按下,这里是我有:

<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中它显示“button标签”。

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

非常感谢。

答:我正在使用

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

我知道这是前一段时间发布,但如果有人正在寻找一个答案,并真的想要使用一个button元素,而不是一个input元素…

不能在IE中使用.attr('value').val()button。 IE报告.val()和.attr(“值”)是button元素的文本标签(内容),而不是value属性的实际值。

您可以通过暂时删除button的标签来解决该问题:

 var getButtonValue = function($button) { var label = $button.text(); $button.text(''); var buttonValue = $button.val(); $button.text(label); return buttonValue; } 

在IE中还有一些其他的怪异button。 我已经为这两个最常见的问题发布了一个修复程序 。

作为一个button值是一个属性,你需要在jQuery中使用.attr()方法。 这应该做到这一点

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

您也可以使用attr来设置属性,更多信息在文档中 。

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

button没有值属性。 要获得button的文本尝试:

 $('.my_button').click(function() { alert($(this).html()); }); 

您也可以使用新的HTML5自定义数据属性。

 <script type="text/javascript"> $(document).ready(function() { $('.my_button').click(function() { alert($(this).attr('data-value')); }); }); </script> <button class="my_button" name="buttonName" data-value="buttonValue">Button Label</button> 

试试这个button:

 <input type="button" class="my_button" name="buttonName" value="buttonValue" /> 

给button一个值属性,然后使用这个检索值:

 $("button").click(function(){ var value=$(this).attr("value"); }); 

受postpostmodern的启发,我做了这个,使.val()在我的JavaScript代码中工作:

 jQuery(function($) { if($.browser.msie) { // Fixes a know issue, that buttons value is overwritten with the text // Someone with more jQuery experience can probably tell me // how not to polute jQuery.fn here: jQuery.fn._orig_val = jQuery.fn.val jQuery.fn.val = function(value) { var elem = $(this); var html if(elem.attr('type') == 'button') { // if button, hide button text while getting val() html = elem.html() elem.html('') } // Use original function var result = elem._orig_val(value); if(elem.attr('type') == 'button') { elem.html(html) } return result; } } }) 

然而,它并没有解决提交问题,后现代解决。 也许这可以被包含在postpostmodern的解决scheme中: http ://gist.github.com/251287

如果你想获得value属性(buttonValue),那么我会使用:

 <script type="text/javascript"> $(document).ready(function() { $('.my_button').click(function() { alert($(this).attr('value')); }); }); </script> 
  <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"> </script> <script> $(document).ready(function(){ $("#hide").click(function(){ $("p").toggle(); var x = $("#hide").text(); if(x=="Hide"){ $("button").html("show"); } else { $("button").html("Hide"); } }); }); </script> </head> <body> <p>If you click on the "Hide" button, I will disappear.</p> <button id="hide">Hide</button> </body> </html>