Javascript 如何在jquery中获取按钮值
无法在jquery中获取按钮值Javascript 如何在jquery中获取按钮值,javascript,jquery,html,Javascript,Jquery,Html,无法在jquery中获取按钮值 <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"> </script> </head> <body> <p> This is para </p> <button> Hide </button>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js">
</script>
</head>
<body>
<p> This is para </p>
<button> Hide </button>
<script>
function handle(){
value = $("button").attr("value");
alert(value);
}
$(function(){
$("button").click(handle);
});
</script>
这是第
隐藏
函数句柄(){
值=$(“按钮”).attr(“值”);
警报(值);
}
$(函数(){
$(“按钮”)。单击(手柄);
});
上面代码的错误是什么。我需要处理由按钮触发的事件,而不是“this”。我已经看到了如何使用“this”来解决它。因此,如果没有“this”,如何处理事件使用jQuery.text()
方法和按钮
元素来获取其中包含的文本值。(例如,打开
和关闭
标记之间的文本。)
您尝试的操作无效,因为按钮
元素没有要检索的值
属性
获取文本示例:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<p> This is para </p>
<button> Hide </button>
<script>
function handle() {
var buttonText = $("button").text();
// Whatever you need to do with the text.
alert(buttonText);
}
$(function(){
$("button").click(handle);
});
</script>
文档:使用jQuery.text()
方法和按钮
元素来获取其中包含的文本值。(例如,打开
和关闭
标记之间的文本。)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<p> This is para </p>
<button> Hide </button>
<script>
function handle() {
var buttonText = $("button").text();
// Whatever you need to do with the text.
alert(buttonText);
}
$(function(){
$("button").click(handle);
});
</script>
您尝试的操作无效,因为按钮
元素没有要检索的值
属性
获取文本示例:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<p> This is para </p>
<button> Hide </button>
<script>
function handle() {
var buttonText = $("button").text();
// Whatever you need to do with the text.
alert(buttonText);
}
$(function(){
$("button").click(handle);
});
</script>
文档:如果要在按钮中获取文本,只需使用:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<p> This is para </p>
<button> Hide </button>
<script>
function handle() {
var buttonText = $("button").text();
// Whatever you need to do with the text.
alert(buttonText);
}
$(function(){
$("button").click(handle);
});
</script>
value=$(“按钮”).text()
而不是value=$(“按钮”).attr(“值”)
您的按钮没有可用于获取数据的值
属性
因此,您应该使用.text()
方法,而不是.attr()
这是第
隐藏
函数句柄(){
值=$(“按钮”).text();
警报(值);
}
$(函数(){
$(“按钮”)。单击(手柄);
});
如果您想在按钮中获取文本,只需使用:
value=$(“按钮”).text()
而不是value=$(“按钮”).attr(“值”)
您的按钮没有可用于获取数据的值
属性
因此,您应该使用.text()
方法,而不是.attr()
这是第
隐藏
函数句柄(){
值=$(“按钮”).text();
警报(值);
}
$(函数(){
$(“按钮”)。单击(手柄);
});
按钮元素没有关联的值属性,因此必须尝试抓取标记中的文本
您可以使用jquery的text()方法获取文本作为
<html>
<head><title>demo</title>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script type='text/javascript'>
$(document).ready(function(){
var x= $('#btn').text();
console.log(x);
});
</script>
</head>
<body>
<button id='btn'>Click Me</button>
</body>
</html>
演示
$(文档).ready(函数(){
var x=$('#btn').text();
控制台日志(x);
});
点击我
按钮元素没有关联的值属性,因此必须尝试抓取标记中的文本
您可以使用jquery的text()方法获取文本作为
<html>
<head><title>demo</title>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script type='text/javascript'>
$(document).ready(function(){
var x= $('#btn').text();
console.log(x);
});
</script>
</head>
<body>
<button id='btn'>Click Me</button>
</body>
</html>
演示
$(文档).ready(函数(){
var x=$('#btn').text();
控制台日志(x);
});
点击我
按钮
没有值
属性…按钮
没有值
属性。。。