如何使用jQuery自动更改按钮文本?
我想更改一个按钮文本:当点击按钮时,它必须显示添加,2-3秒后,我需要它显示添加 下面是一个示例代码:如何使用jQuery自动更改按钮文本?,jquery,schedule,jquery-events,Jquery,Schedule,Jquery Events,我想更改一个按钮文本:当点击按钮时,它必须显示添加,2-3秒后,我需要它显示添加 下面是一个示例代码: jQuery(document).ready(function($) { $('.addcartbtn').toggle(function() { $(this).val('Adding'); }, // How to make the button text automatically change after 3 seconds? fun
jQuery(document).ready(function($) {
$('.addcartbtn').toggle(function() {
$(this).val('Adding');
},
// How to make the button text automatically change after 3 seconds?
function() {
$(this).val('Added');
});
});
$('.addcartbtn').on("click",function(){
$(this).val("Adding....");
setTimeout(function(){
$('.addcartbtn').val("Added");}, 2000);
});
您可以这样做:
$(function () {
var that = $('.addcartbtn'); // cache the element
that.text('Adding..'); // change its text to "Adding.."
setTimeout(function () { // create a function to be executed after...
that.text('Added');
}, 3000); // ... 3 seconds
});
您可以这样做:
$(function () {
var that = $('.addcartbtn'); // cache the element
that.text('Adding..'); // change its text to "Adding.."
setTimeout(function () { // create a function to be executed after...
that.text('Added');
}, 3000); // ... 3 seconds
});
我为你做了一把小提琴。这里有一个例子,有两种情况下你可能需要,因为你没有正确地解释你有哪种按钮。看看
JS
HTML
如果你有一个输入标签
<input type="submit" class="addcartbtn" value="Click Me" />
如果你有一个按钮标签
<button class="addcartbtn">Click me</button>
我为你做了一把小提琴。这里有一个例子,有两种情况下你可能需要,因为你没有正确地解释你有哪种按钮。看看
JS
HTML
如果你有一个输入标签
<input type="submit" class="addcartbtn" value="Click Me" />
如果你有一个按钮标签
<button class="addcartbtn">Click me</button>
请尝试以下简单代码:
jQuery(document).ready(function($) {
$('.addcartbtn').toggle(function() {
$(this).val('Adding');
},
// How to make the button text automatically change after 3 seconds?
function() {
$(this).val('Added');
});
});
$('.addcartbtn').on("click",function(){
$(this).val("Adding....");
setTimeout(function(){
$('.addcartbtn').val("Added");}, 2000);
});
请尝试以下简单代码:
jQuery(document).ready(function($) {
$('.addcartbtn').toggle(function() {
$(this).val('Adding');
},
// How to make the button text automatically change after 3 seconds?
function() {
$(this).val('Added');
});
});
$('.addcartbtn').on("click",function(){
$(this).val("Adding....");
setTimeout(function(){
$('.addcartbtn').val("Added");}, 2000);
});
一些HTML代码会被欣赏,你所做的听起来像是伪造功能。是否有任何操作之后按钮标签应该更改?依赖计时功能不是最好的主意,相反,你可以听事件/用户的动作。嗨@feeela,是的,不是最好的主意,但我需要简单的解决方案。这条路最适合我。谢谢你的建议。一些HTML代码会被欣赏。你所做的听起来像是伪造功能。是否有任何操作之后按钮标签应该更改?依赖计时功能不是最好的主意,相反,你可以听事件/用户的动作。嗨@feeela,是的,不是最好的主意,但我需要简单的解决方案。这条路最适合我。谢谢你的建议。那不是他需要的!忘记超时了。我的错那不是他需要的!忘记超时了。我的错误很高兴能帮助您:@ErdalBakkali需要在自动更改后单击按钮..例如按钮测试默认签出。。当我点击添加。。Addedglad帮助您:@ErdalBakkali需要在自动更改后单击按钮。例如,按钮测试默认签出。。当我点击添加。。Added这将在页面上的每个.addcartbtn中设置Added,而不仅仅是在单击的页面上。这将在页面上的每个.addcartbtn中设置Added,而不仅仅是在单击的页面上。