使用 jquery 禁用 input type="image" 的正确方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2211652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How is the correct way to disable an input type="image" using jquery?
提问by systempuntoout
With jQuery, I'm trying to disable an input field like this:
使用 jQuery,我试图禁用这样的输入字段:
<input id="submit" type="image" src="submit.jpg">
What I would like to do is disabling the button and change the image with a different image (submitGreyed.jpg) to visually notify that button is disabled.
我想要做的是禁用按钮并使用不同的图像(submitGreyed.jpg)更改图像以直观地通知该按钮已禁用。
With the following line I disable the button:
通过以下行,我禁用了按钮:
JQuery("#submit").attr('disabled','true');
then I change the image with:
然后我更改图像:
JQuery("#submit").attr('src','submitGreyed.jpg');
and once disabled I submit the form with:
一旦禁用,我提交表单:
JQuery("#form").submit();
The second line has some weird behaviour; sometimes works and sometimes doesn't.
第二行有一些奇怪的行为;有时有效,有时无效。
When it works, button is disabled, image changed and form is submitted; when it does not work, button is disabled, form is submitted but image is not changed.
当它工作时,按钮被禁用,图像改变并提交表单;当它不起作用时,按钮被禁用,表单被提交但图像不会改变。
How can I solve this?
我该如何解决这个问题?
回答by Doug Neiner
This doesn't answer your question exactly, but I hope it helps:
这并不能完全回答您的问题,但我希望它有所帮助:
First, it should be disabled="disabled"
so use this:
首先,应该disabled="disabled"
这样使用:
jQuery("#submit").attr('disabled','disabled');
And I am not sure what your grayed out button looks like, but you could try just using opacity:
而且我不确定您的灰色按钮是什么样的,但您可以尝试仅使用不透明度:
jQuery("#submit").attr('disabled','disabled').css('opacity',0.5);
UpdateI couldn't replicate the problem, so here is my suggestion:
更新我无法复制这个问题,所以这是我的建议:
Use an absolute path to the image instead of a relative one, and set both attributes at the same time (Though setting one after the other didn't change my test):
使用图像的绝对路径而不是相对路径,并同时设置两个属性(虽然一个接一个设置并没有改变我的测试):
jQuery("#submit").attr({
disabled: 'disabled',
src: '/images/submitGreyed.jpg'
});
Since in my test I used a full path, that might have affect it a bit.
由于在我的测试中我使用了完整路径,这可能会对它产生一些影响。