IE7 / IE8 与 JQuery .removeAttr('disabled') 交互,不应用 css
这更像是一个好奇心,而不是一个问题,但我想知道为什么以下内容不起作用。
假设我有一个带有选择器和提交按钮的 html 表单。
<form action="/foo/createActivity" method="post" name="activityform" id="activityform" >
<select name="activity" id="activity" >
<option value="1" >Activity 1</option>
<option value="2" >Activity 2</option>
</select>
<input type="submit" class="submit" id="add" value="Add >>"/>
</form>
然后我有一个定义了 jQuery 的 ajax 调用,
$('#activity').change(function() {
tryDisable($('#activity').val());
});
function tryDisable(activity) {
$.ajax({
type: 'GET',
contentType: 'application/json',
cache: false,
url: '/foo/checkActivity',
data: {
activity: activity
},
success: function(disableSubmit) {
if (disableSubmit == "true") {
$('#add').attr('disabled', 'true');
} else {
$('#add').removeAttr('disabled'); // Problem in IE, doesn't take away CSS of disabled
// $('#add').css({"text-align" : "center"});
// NO idea why the above fixes the problem, TODO find out
}
},
dataType: 'json'
});
return false;
}
您将在我的评论中看到,只是 .removeAttr('disabled') 导致按钮重新启用,但按钮仍然看起来就像被禁用一样(灰显)。但是,如果我像在注释掉的行中那样通过 jquery 'tickle' css,则应用正确的、非禁用的样式。正如我所料,Firefox 和 Chrome 仅使用第一行就可以正常工作。
有谁知道为什么会这样? IE 在这里做什么如此奇怪?
This is more of a curiosity than a question, but I'm wondering why the following isn't working.
Say I have an html form with a selector and a submit button.
<form action="/foo/createActivity" method="post" name="activityform" id="activityform" >
<select name="activity" id="activity" >
<option value="1" >Activity 1</option>
<option value="2" >Activity 2</option>
</select>
<input type="submit" class="submit" id="add" value="Add >>"/>
</form>
Then I have an ajax call with jQuery defined
$('#activity').change(function() {
tryDisable($('#activity').val());
});
function tryDisable(activity) {
$.ajax({
type: 'GET',
contentType: 'application/json',
cache: false,
url: '/foo/checkActivity',
data: {
activity: activity
},
success: function(disableSubmit) {
if (disableSubmit == "true") {
$('#add').attr('disabled', 'true');
} else {
$('#add').removeAttr('disabled'); // Problem in IE, doesn't take away CSS of disabled
// $('#add').css({"text-align" : "center"});
// NO idea why the above fixes the problem, TODO find out
}
},
dataType: 'json'
});
return false;
}
You'll see in my comments that just the .removeAttr('disabled') is causing the button to re-enable, but the button still looks like it's disabled (gray-ed out). However, if I 'tickle' the css via jquery as I did in the commented out line, the correct, non-disabled styling applies. Firefox and Chrome work fine with just the first line, as I expected.
Does anyone know why this might be? What's IE doing here that's so odd?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我以前也遇到过同样的问题。我用谷歌搜索并从 jQuery 网站 找到了这个注释。
注意:尝试使用
.removeAttr()
删除内联onclick
事件处理程序在 Internet Explorer 6、7、或 8. 为了避免潜在的问题,请使用.prop()
代替:我还在 SO 中找到了几篇关于
.removeAttr()
的帖子:removeAttr()
问题.prop('checked',false)
或.removeAttr('checked')
?这个 帖子也很有帮助。
I did have the same problem before. I googled it and found this note from jQuery website.
Note: Trying to remove an inline
onclick
event handler using.removeAttr()
won't achieve the desired effect in Internet Explorer 6, 7, or 8. To avoid potential problems, use.prop()
instead:I also found a couple of posts here in SO regarding the
.removeAttr()
:removeAttr()
issue.prop('checked',false)
or.removeAttr('checked')
?This post by the John Resig is also helpful.