Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.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
如何使用jquery更改单击时输入按钮的文本?_Jquery_Button - Fatal编程技术网

如何使用jquery更改单击时输入按钮的文本?

如何使用jquery更改单击时输入按钮的文本?,jquery,button,Jquery,Button,我想在单击按钮时将以下按钮的添加文本更改为“删除” <input class="btn btn-6d" ahref="www.test.com" type="button" value="ADD"/> 我的选择器可能有问题,但我对编程比较陌生,不知道我做错了什么 这里有一把小提琴供您弹奏: 提前感谢, Stijn您使用的是value()而不是val()。下面的任何内容都只是一点清理:) JSFiddle: 简单一点,您只需要将类用作选择器。还要注意,方法名称是.val(),而不是

我想在单击按钮时将以下按钮的添加文本更改为“删除”

<input class="btn btn-6d" ahref="www.test.com" type="button" value="ADD"/>
我的选择器可能有问题,但我对编程比较陌生,不知道我做错了什么

这里有一把小提琴供您弹奏:

提前感谢,

Stijn

您使用的是
value()
而不是
val()
。下面的任何内容都只是一点清理:)

JSFiddle:


简单一点,您只需要将类用作选择器。还要注意,方法名称是
.val()
,而不是
.value()

$('.btn.btn-6d')。单击(函数(){
var$this=$(this);
$this.toggleClass('btn-6d');
如果($this.hasClass('btn-6d')){
$this.val('ADD');//方法名为.val()
}否则{
$this.val('REMOVE');
}
});


不要忘记在JSFIDLE中包含jquery插件。你可以在左边的面板上,首先下拉。谢谢!这解决了问题,我在这里学到了一些有用的东西。
$(':input[type="button"][class="btn-6d"]').click(function(){
var $this = $(this);
$this.toggleClass('btn-6d');
if($this.hasClass('btn-6d')){
    $this.value('ADD');         
} else {
    $this.value('REMOVE');
}
});
   // Find the element by the generic btn class
    $(document).on('click', '.btn:input[type="button"]', function(){
        var $this = $(this);
        $this.toggleClass('btn-6d');
        if($this.hasClass('btn-6d')){
            $this.val('ADD');           
        } else {
            $this.val('REMOVE');
        }
    });