jquery's attr function works only one time

旧巷老猫 提交于 2019-12-13 02:27:35

问题


I use this snippet to show a dialog. This works great however: the title is going to be set only for the first time I click the table cell. After reloading the page again the title is set - for one time. Ad infinitum...

$(document).ready(function() {
    $("td[id^='_ctl0_tbl_content_reportid_']").click(function() {
        var tokens = this.id.split('_');
        var last_index = tokens.length - 1;
        var _dialog = $("#reportid_dialog_" + tokens[last_index]);
        var _title = _dialog.attr("title");

        _dialog.dialog({
            modal: true,
            closeText: 'Hide',
            width: 450,
            title: _title
        });
    });
)};

I use jQuery 1.4.2 with jQuery-ui 1.8.2 Maybe there is somebody to tell me what I'm doing wrong.


回答1:


dialog() moves the element you're passing to it, so that it's no longer matched by the selector. Might that be it?




回答2:


I think your running into a dual selector issue. Since your selecting:

$("#reportid_dialog_" + tokens[last_index]);

The second time you click on the table cell and call dialog() there is 2 instances of #reportid_dialog_ which would lead to id's clashing when you attempt to set attrs



来源:https://stackoverflow.com/questions/3306414/jquerys-attr-function-works-only-one-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!