$(this).attr(“id”) not working

前端 未结 9 745
情书的邮戳
情书的邮戳 2020-12-30 02:55

as the title says, I keep getting \"undefined\" when I try to get the id attribute of an element, basically what I want to do is replace an element with an input box when th

9条回答
  •  执念已碎
    2020-12-30 03:40

    Change

    var ID = $(this).attr("id");
    

    to

    var ID = $(obj).attr("id");
    

    Also you can change it to use jQuery event handler:

    $('#race').change(function() {
        var select = $(this);
        var id = select.attr('id');
        if(select.val() == 'other') {
            select.replaceWith("");
        } else {
            select.hide();
        }
    });
    

提交回复
热议问题