最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Add disabled style (css) to input type file button - Stack Overflow

programmeradmin0浏览0评论

I am disabling a input file button in JQuery which works

$('#ID').attr('disabled', true)

However, the button still looks enabled and doesn't show the disabled style (grey)

I have tried changing the CSS

// Example 
$('#ID').css("background-color", 'yellow')

But regardless of what css I put the button never changes style.

What I can do?

the object I am using (HTML)

<input type="file" id="ID" class="" name="files[]" multiple />

Thanks

I am disabling a input file button in JQuery which works

$('#ID').attr('disabled', true)

However, the button still looks enabled and doesn't show the disabled style (grey)

I have tried changing the CSS

// Example 
$('#ID').css("background-color", 'yellow')

But regardless of what css I put the button never changes style.

What I can do?

the object I am using (HTML)

<input type="file" id="ID" class="" name="files[]" multiple />

Thanks

Share Improve this question asked Jun 24, 2016 at 9:53 user3428422user3428422 4,59013 gold badges64 silver badges131 bronze badges 4
  • Under what circumstances are you disabling the button? This seem to work fine - codepen.io/Paulie-D/pen/JKEKmE – Paulie_D Commented Jun 24, 2016 at 9:58
  • What browser is this? It looks greyed out disabled in chrome and firefox to me. Maybe try and restyle the whole thing to get a dramatic result - tympanus/codrops/2015/09/15/… – dmoo Commented Jun 24, 2016 at 9:59
  • @Paulie_D - I need the button disabled when the user is on a tablet, phone, device etc. its strange your example shows it more "greyed out" then what I see in Chrome. Cheers – user3428422 Commented Jun 24, 2016 at 10:27
  • @user3428422 quirksmode/dom/inputfile.html – Gautam Jha Commented Jun 24, 2016 at 10:31
Add a ment  | 

3 Answers 3

Reset to default 1

Use prop instead attr:

 $("#id").prop("disabled",true);

https://jsfiddle/8fvga3xk/8/

Sorry for previous answer.

I suggest you to check this link to know possibility about changing a input file style.

div.fileinputs {
	position: relative;
}

div.fakefile {
	position: absolute;
	top: 0px;
	left: 0px;
	z-index: 1;
}

input.file {
	position: relative;
	text-align: right;
	-moz-opacity:0 ;
	filter:alpha(opacity: 0);
	opacity: 0;
	z-index: 2;
}
<div class="fileinputs">
	<input type="file" class="file" disabled/>
	<div class="fakefile">
		<input disabled/>
		<img src="http://fptp.uthm.edu.my/mba/images/911c660826c0b.png"  style="width:30px;"/>
	</div>
</div>

instead of true you can use disabled;

$("#ID").attr('disabled','disabled');

or you can use .prop()

$("#ID").prop('disabled', true);
$("#ID").prop('disabled', false);

You can use the CSS selector :disabled http://www.w3schools./cssref/sel_disabled.asp

.yourButtonClass:disabled{
   background-color:#dddddd;
}
发布评论

评论列表(0)

  1. 暂无评论