问题描述
这更像是一个好奇而不是一个问题,但我想知道为什么以下不起作用。
This is more of a curiosity than a question, but I'm wondering why the following isn't working.
假设我有一个带选择器的html表单和提交按钮。
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>
然后我有一个jQuery定义的ajax调用
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;
}
你会在我的评论中看到.removeAttr('禁用' )导致按钮重新启用,但按钮仍然看起来像它被禁用(灰色显示)。但是,如果我像在注释掉的行中那样通过jquery'发送'css,则应用正确的,非禁用的样式。正如我所料,Firefox和Chrome只在第一行正常工作。
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.
有谁知道为什么会这样? IE在这做什么,这太奇怪了?
Does anyone know why this might be? What's IE doing here that's so odd?
推荐答案
之前我确实遇到过同样的问题。我用Google搜索并在上找到了这个说明。
I did have the same problem before. I googled it and found this note from jQuery website.
注意:尝试使用删除内联
将无法在Internet Explorer 6,7或8中实现所需效果。为避免潜在问题,请改为使用 onclick
事件处理程序.removeAttr() .prop()
:
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:
$element.prop("onclick", null);
console.log("onclick property: ", $element[0].onclick); // --> null
我还在SO中发现了一些关于 .removeAttr的帖子()
:
I also found a couple of posts here in SO regarding the .removeAttr()
:
removeAttr()
issue.prop('checked',false)
or.removeAttr('checked')
?
这也很有帮助。
这篇关于IE7 / IE8与JQuery .removeAttr('禁用')交互,不应用css的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!