我有一张桌子,我想在点击按钮时隐藏或显示。此外,单击该按钮时,应适当更改其文本。我有以下代码,但按钮的文本没有被更改:
<script>
$(document).ready(function () {
$("#myButton").click(function () {
$(".myTable").toggle(1000, "linear", function changeButtonText() {
$("#myButton").text = ($("#myButton").text === "Hide table" ? "Show table" : "Hide table");
});
});
});
</script>
...
<input type="button" id="myButton" value="Hide table" />
<table class="myTable">
...
</table>
答案 0 :(得分:3)
试试这个:
getInfoWindow
答案 1 :(得分:1)
使用它来更改按钮的文本
$("#myButton").attr('value', 'newText');
答案 2 :(得分:1)
您没有以正确的方式使用该功能:
如果元素为按钮:
错误:
$("#myButton").text = ("new text");
工作:
$("#myButton").text("new text");
工作示例:
$("#myButton").text("New text");
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button" id="myButton">Old text</button>
&#13;
如果元素为输入类型=&#34;按钮&#34; :
错误:
$("#myButton").text = ("new text");
工作:
$("#myButton").val("new text");
工作示例:
$("#myButton").val("New text");
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" id="myButton" value="Hide table" />
&#13;
答案 3 :(得分:0)
我必须调整你的代码并为我工作。你必须通过像$("#myButton").val(Buttontext);
$(document).ready(function() {
$("#myButton").click(function() {
$(".myTable").toggle(1000, "linear", function changeButtonText() {
var text= ($("#myButton").val() === "Hide table" ? "Show table" : "Hide table");
$("#myButton").val(text);
});
});
});
fiddle为我工作
答案 4 :(得分:0)
您可以使用if语句检查按钮的值,但首先必须获取值为的属性。
$("#myButton").click(function () {
$(".myTable").toggle(1000, "linear", function changeButtonText() {
var text = $("#myButton").attr("value");
if(text == "Hide table") {
$("#myButton").attr("value","Show table");
}
else {
$("#myButton").attr("value","Hide table");
}
});
});
答案 5 :(得分:0)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
...
<button id="myButton">Hide table</button>
<table class="myTable">
<tr><td>works</td></tr>
</table>
<script>
$("#myButton").click(function () {
if($("#myButton").text() == "Hide table") {
$(".myTable").hide();
$("#myButton").text("Show table");
}
else {
$(".myTable").show();
$("#myButton").text("Hide table");
}
});
</script>
将javascript放在您身体的底部,删除适用于我的就绪功能