简体   繁体   English

如何正确禁用IE10中的按钮?

[英]How to properly disable a button in IE10?

Guys I have tested this code in Chrome, Firefox, IE 7 8 and 9. Only in IE10 it doesn't work. 伙计们,我已经在Chrome,Firefox,IE 7 8和9中测试了此代码。只有在IE10中它才起作用。

 $("#upload_file").live('click', function() {
     $('#upload_file').prop('disabled', 'disabled');

     //then do some more codes like ajax
 }

When I click the upload button, it should disable that button to avoid double clicks. 当我单击上载按钮时,它应禁用该按钮,以避免双击。 This works properly on other browsers except in IE10. 此功能在IE10以外的其他浏览器上均可正常使用。 in IE10 it doesn't look disabled, but it wont execute the other codes below it. 在IE10中,它看起来没有被禁用,但是它不会执行下面的其他代码。 So I'm assuming it really disabled the functionality, not the button 所以我假设它确实禁用了功能,而不是按钮

Disabled should be set to a boolean value: 禁用应设置为布尔值:

$('#upload_file').prop('disabled', true);

As per the jQuery docs : 根据jQuery文档

The .prop() method should be used to set disabled and checked instead of the .attr() method. 应该使用.prop()方法而不是.attr()方法来设置禁用和检查状态。

$( "input" ).prop( "disabled", false );

It seams to me that your problem came from using old version of jQuery. 在我看来,您的问题来自使用旧版本的jQuery。 Because IE 10 recently created while your jQuery i guess is 1.6 which is not optimized for new version of browsers. 因为我最近在您的jQuery上创建了IE 10,所以我猜它是1.6,它并未针对新版本的浏览器进行优化。 I strongly suggest to you update to new version, unless you are re-factoring existing code. 我强烈建议您更新到新版本,除非您要重构现有代码。

Try this with .on() with event delegation: 通过事件委托与.on()一起尝试:

 $(document).on('click', '#upload_file', function() {
     $(this).prop('disabled', true);
     //then do some more codes like ajax
 });

you can use closest static parent to the target element("_#upload_file_") that may be a <div>, <table> which holds the button. 您可以使用最接近目标元素(“ _#upload_file_”)的静态父元素,目标元素可以是保存按钮的<div>, <table>

To re-enable the button: 要重新启用按钮:

 $(document).on('click', '#upload_file', function() {
     $(this).prop('disabled', true);
     $.ajax({
         url: your url,
         type: your type,
         .......
         success:function(){

         },
         complete:function(){
            $('#upload_file:disabled').prop('disabled', false); //<----here
         },
         error: function(){}
     });
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM