duanming7833 2016-09-29 10:30
浏览 96
已采纳

.find()不适用于某些id

In my website the clients can update their personal data. After the update is done on the database, I want the page updates itself and show the data as it is at that time. For doing this, I do an ajax call to an external php file to update the mysql database. After that, I do a get of the client data page (location.href) and I update some div info to show up the new data.

This is the AJAX call from my JS file:

$.ajax({
    type: "POST",
    url: "factupdate.php",
    data: {
        datos: stringDatos, 
        token: $("#token").val(), 
        isAjax: 1
    },
    success: function (codi) {
        $.get(location.href, function (datos) {
            ($(datos)).find("#eltoken").html());
            var eltoken = $(datos).find("#eltoken");
            var result = $(datos).find("#cuenta_menu_fac");
            console.log($(eltoken).html());
            console.log($(result).html());
            $(result).find(".facturacion").hide();
            $(result).find("#fac_" + num).show();
            $(result).find(".afac").removeClass("aliselected");
            $(result).find("#menu_fac_" + num).addClass("aliselected");
            $(result).find("#acc_fac").append('<span class="goodvalid">Datos de facturación actualizados</span>');
            $("#cuenta_menu_fac").html($(result).html());
            $("#eltoken").html($(datos).find("#eltoken").html());
            $("#cargandoacc").hide();
        });
    },
    error: function (e) {
        $("#cargandoacc").hide();
        alert('Ha habido un error: ' + e);
    }
});

The problem is that I can see the result data but not the eltoken's, the console prints undefined.

Moreover, when I try to do a console.log($(datos).html()); it also shows up as undefined. On the other hand, if I do a console.log($(datos).text()); it does return the correct data of the website.

This is some of datos info:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="es" xml:lang="es">
<head>
...
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
...
</head>
<body>
<!-- Some layout divs -->



<div id="cuenta_menu_fac" class="c-menu-content">
<div class="content" id="acc_fac">

<!-- Some label and input for updating data divs -->

</div>
</div>




<!-- /Some layout divs -->
<div id="eltoken">
<input type="hidden" id="token" value="2d5951d1e3b31dfb7fd2dcc172df17fd">
</div>
</body></html>

I do not understand what is wrong in my code. Any possible solution to this?

  • 写回答

1条回答 默认 最新

  • du060334 2016-09-29 12:45
    关注

    Datos is not a parent for eltoken. Just add eg. a common div for it.

    $.get(location.href, function (datos) {
        datos = $('<div></div>').append(datos);
        ($(datos)).find("#eltoken").html());
        ...
    

    See my similar example https://jsfiddle.net/011g253w/1/

    Update

    Your code has 2 divs on one level in the body. And get returns it as a result of selector $(*, 'body') = array of 2 divs.

    I found a better way for your case.

    var eltoken = $(datos).filter("#eltoken");
    var result = $(datos).filter("#cuenta_menu_fac");
    

    https://jsfiddle.net/011g253w/2/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型