Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 更改jQuery中的按钮文本_Javascript_Jquery_Html - Fatal编程技术网

Javascript 更改jQuery中的按钮文本

Javascript 更改jQuery中的按钮文本,javascript,jquery,html,Javascript,Jquery,Html,我有一个表,我想隐藏或显示一个按钮点击。此外,单击按钮时,应适当更改其文本。我有以下代码,但按钮的文本未被更改: <script> $(document).ready(function () { $("#myButton").click(function () { $(".myTable").toggle(1000, "linear", function changeButtonText() {

我有一个表,我想隐藏或显示一个按钮点击。此外,单击按钮时,应适当更改其文本。我有以下代码,但按钮的文本未被更改:

<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>

$(文档).ready(函数(){
$(“#我的按钮”)。单击(函数(){
$(“.myTable”).toggle(1000,“线性”,函数changeButtonText(){
$(“#我的按钮”).text=($(“#我的按钮”).text==“隐藏表格”?“显示表格”:“隐藏表格”);
});
});
});
... 
...

使用此选项更改按钮的文本

$("#myButton").attr('value', 'newText');

您没有以正确的方式使用该函数:


  • 如果元素为按钮:

错误:

$("#myButton").text = ("new text");
工作:

工作示例:

$(“#我的按钮”).text(“新文本”)

旧文本
尝试以下操作:

$("#myButton").val($("#myButton").val() === "Hide table" ? "Show table" : "Hide table");

我必须调整你的代码并为我工作。你必须像
$(“#myButton”).val(Buttontext)那样通过vale


为我工作

您可以使用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");
      }
    });
});

... 
隐藏表
作品
$(“#我的按钮”)。单击(函数(){
如果($(“#myButton”).text()=“隐藏表格”){
$(“.myTable”).hide();
$(“#我的按钮”).text(“显示表格”);
} 
否则{
$(“.myTable”).show();
$(“#我的按钮”).text(“隐藏表格”);
}
});

应该是
$(“#myButton”).text()
而不仅仅是
$(“#myButton”).text
.text()
是函数它是一个
输入
标记,而不是
按钮
不,我的不行
$(“#myButton”).val(…)
,您有
$(“#myButton”).val()=…
@Waxi可能您没有看到我的答案^^^^@ion,但您通过将值传递到
val()
方法来设置值,而不是通过为
val()
方法赋值。@Waxi感谢您的澄清,他很难理解:)@Arg0n谢谢。这确实是唯一可行的答案。
$(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);
    });
  });
});
$("#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");
      }
    });
});