无法获取元素ID(仅在特定表中) - 返回undefined

时间:2014-01-13 14:29:04

标签: javascript php jquery html

我正在尝试获取元素ID,例如<tr><td>,当我调用$('element').id;$('element').attr('id');时,我得到了未定义:(

我做错了吗?

有人回答并删除了:

我可以改为:

$(this).closest('tbody > tr').attr('id')

它会起作用!

感谢!!!!!!

该表是PHP生成的。

Snippet(注意<tr id=\"". $info['id'] ."\">,这就是我想要的):

<!--  TAB 3 (READ) -->
<div id="read" class="span9">
    <div id="deleteForm"></div>
    <table id="db_table" class="table table-hover">
        <thead>
        <tr>
            <th>#</th>
            <th>Nome</th>
            <th class="width_adjust">Pre&ccedilo</th>
            <th>Ingredientes</th>
            <th></th>
        </tr>
        </thead>
        <tbody>
        <?php
        $data = mysql_query("SELECT * FROM cardapio")
            or die(mysql_error());

        while($info = mysql_fetch_array( $data )){
            $info['preco'] = str_replace('.', ',', $info['preco']);
            print("

                 <tr id=\"". $info['id'] ."\">
                    <td>".$info['id']."</td>
                    <td>".$info['nome']."</td>
                    <td>R$ ".$info['preco']."</td>
                    <td>".$info['descricao']."</td>
                    <td><a class=\"deleteRow\" href=\"/cadastro.php?id=". $info['id'] ."\"><button class=\"close\">&times;</button></td>
                </tr>

            "); } ?>
        </tbody>
    </table>
</div>

我正在尝试,当用户点击<a>元素时,会弹出一个确认对话框,如果单击“确定”,则会从DB / html中删除所需的行。

我正在尝试做什么(javaScript):

// Delete row in READ tab
    $('a.deleteRow').click(function(e) {
        var x = confirm("Are you sure?");
        if (x==true){
            //Ajax for delete entry from DB
            var id_var = $(this).closest('tr').id;
            var xhr;
            if (window.XMLHttpRequest) xhr = new XMLHttpRequest(); // all browsers
            else xhr = new ActiveXObject("Microsoft.XMLHTTP");  // for IE

            var url = '/components/delete.php?id_var=' + id_var;
            xhr.open('GET', url, false);
            xhr.onreadystatechange = function () {
                if (xhr.readyState===4 && xhr.status===200) {
                    var div = document.getElementById('deleteForm');
                    div.innerHTML = xhr.responseText;
                }
            }
            xhr.send();
            //FadeOut to delete from HTML
            $(this).closest('tr').fadeOut();
        }else{
            //e.preventDefault(); // don't follow the link
        }
    });

示例图像(使用table元素进行测试): http://i.stack.imgur.com/kgWU3.png

呈现HTML: http://i.stack.imgur.com/JFcFP.png http:// pastebin.com/hZLxRL0V

2 个答案:

答案 0 :(得分:0)

此代码:

var id_var = $(this).closest('tr').id;

几乎存在,但是你想要jQuery结果集中第一个元素的标识符,而不是结果集本身的.id属性(未定义):

var id_var = $(this).closest('tr').prop('id');

答案 1 :(得分:0)

演示:http://jsfiddle.net/nc4j8/

<table id='table'>
    <tr data-id='1'>
        <td>Hello</td>
        <td><a class='deleteRow'>delete</a></td>
    </tr>
    <tr data-id='2'>
        <td>Cześć</td>
        <td><a class='deleteRow'>delete</a></td>
    </tr>
    <tr data-id='3'>
        <td>Good morning</td>
        <td><a class='deleteRow'>delete</a></td>
    </tr>
</table>
$('#table').on('click', '.deleteRow', function(){
    var $tr = $(this).parents('tr');
    var id = $tr.attr('data-id');
    $tr.fadeOut('fast');
});