problem in code when update jquery
i have this code. Works well with jquery 1.5.2, now i updated to 1.6 and the the delete button doesn't work
here works well, but here, the button delete doesn't work
<script type="text/javascript">
$(document).ready(function() {
$('#btnAdd').click(function() {
var num = $('.clonedInput').length;
var newNum = new Number(num + 1);
var newElem = $('#input' + num).clone().attr('id', 'input' + newNum);
newElem.children(':first').attr('id', 'name' + newNum).attr('name', 'name' + newNum);
$('#input' + num).开发者_开发知识库after(newElem);
$('#btnDel').attr('disabled','');
if (newNum == 5)
$('#btnAdd').attr('disabled','disabled');
});
$('#btnDel').click(function() {
var num = $('.clonedInput').length;
$('#input' + num).remove();
$('#btnAdd').attr('disabled','');
if (num-1 == 1)
$('#btnDel').attr('disabled','disabled');
});
$('#btnDel').attr('disabled','disabled');
});
</script>
Any idea?
I think in jquery 1.6 attr() function has changed and in some cases you should use prop(). In 1.6.1 they re-introduced some changes for compatibilty. look here for prop() specification
Taken from jquery site:
The difference between attributes and properties can be important in specific situations. Before jQuery 1.6, the .attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. As of jQuery 1.6, the .prop() method provides a way to explicitly retrieve property values, while .attr() only retrieves attributes.
The key point is that you have to use attr()
correctly. disabled
is a boolean attribute, to enable an element you must remove it, not set it to the empty string. See this: http://jsfiddle.net/zjcfN/
That prop()
works is more of an accident than anything else. You can (and should!) continue to use attr()
to work with HTML element attributes, and as of jQuery 1.6 you can additionally use prop()
to work with DOM object properties.
The following works perfectly in jQuery 1.6:
// disable an element
$("input").attr("disabled", "disabled");
// enable an element
$("input").removeAttr("disabled");
精彩评论