ID未定义但不是?

时间:2011-11-21 20:06:33

标签: javascript attributes set undefined

请看这个小提琴:http://jsfiddle.net/DHts6/1/

为什么说未定义,即使设置了id = "foo"

由于

3 个答案:

答案 0 :(得分:4)

您不需要$(this).id,只需执行this.id

原因是$创建了一个没有.id属性的jQuery对象。在jQuery中,您可以使用attr $(this).attr('id')

http://jsfiddle.net/DHts6/5/

答案 1 :(得分:1)

使用this.id $(this).id

http://jsfiddle.net/maniator/DHts6/2/

答案 2 :(得分:0)

$(document).ready(function(){
$('b.edit').click(function(){
        $(this).hide().after('<form action = "foo.php" method = post><input name = "field" type = "text" value = "'+$(this).attr('id')+'"/><input type="text" name = "period" class="editP" value="'+$(this).html()+'" /><input type = "submit" value = "Submit!!" /></form>');
        $('.editP').focus();
    });
    $('.editP').live('blur', function(){
        $(this).hide().prev('b.edit').html($(this).val()).show();
    });
});