获取具有相同名称但值不同的两个按钮之一的值

时间:2018-02-04 17:24:32

标签: jquery html button

我试图从html代码中给出的同名但不同的值中取出一个按钮的值。如何使用jQuery获取我们点击的按钮的值?



<html>
<input type="button" name="btn1" value="Button1" text="Button1">
<input type="button" name="btn1" value="Button2" text="Button2">
<input type="button" name="btn1" value="Button3" text="Button3">
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您可以从attribute获取input button类型文字并显示。

&#13;
&#13;
$("input").click(function(){
  var value=$(this).attr("text");
  console.log(value);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<input type="button" name="btn1" value="Button1" text="Button1">
<input type="button" name="btn1" value="Button2" text="Button2">
<input type="button" name="btn1" value="Button3" text="Button3">
</html>
&#13;
&#13;
&#13;

相关问题