问题描述
我正在使用表单和jQuery在网站上进行快速更改.我想将按钮文本更改为已保存!"然后在几秒钟后将其更改回更新",以便用户可以再次更改该值.当然,他们可以点击现在的已保存!"再按一次按钮,但看起来不太好.
I'm using a form and jQuery to make a quick change on a web site. I would like to change the button text to 'Saved!' then change it back to Update after a few seconds so the user can change the value again. Of course they can hit the now 'Saved!' button again, but it doesn't look nice.
$("form.stock").submit(function(){
// Example Post
$.post($(this).attr('action'), { id: '123', stock: '1' });
$(this).find(":submit").attr('value','Saved!');
// This doesn't work, but is what I would like to do
setTimeout($(this).find(":submit").attr('value','Update'), 2000);
return false;
});
推荐答案
setTimeout的第一个参数是函数.因此,将您的代码包装在一个匿名函数中,就可以了.
First argument to setTimeout is function. So wrap your code inside an anonymous function and you are good to go.
$("form.stock").submit(function(){
// Example Post
$.post($(this).attr('action'), { id: '123', stock: '1' });
var submit = $(this).find(":submit").attr('value','Saved!'); //Creating closure for setTimeout function.
setTimeout(function() { $(submit).attr('value','Update') }, 2000);
return false;
});
我现在无法测试此代码.让我知道它是否无效.
I am not able to test this code right now. Let me know if it doesn't work.
编辑:正如redsquare所建议的那样,从提交"按钮本身创建闭包是有意义的.
As suggested by redsquare, it makes sense to create closure from the submit button itself.
这篇关于几秒钟后使用jQuery将输入按钮文本更改回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!