doukezi4576 2015-09-07 07:04
浏览 31

使用jQuery从handler中查找#ID

How can I find an #[ID] using jQuery?

I have a handler that checks some info, I'm checking with jQuery if there is found a ID... But it always give back "Nothing" and it gives me the errors so it mean that there are errors!

I do it on this way...

jQuery:

posting.done(function(data){
    var warnings = $(data).find('.warnings');

    $('form#gastenboek').append(data);

    if (warnings.length > 0) {
        alert("errors")
    } else {
        alert("Nothing");
    }
});

Handler:

if (!empty($warning) && isset($warning))
{
    foreach($warning as $warning)
    {
        echo "<span id='warnings' style='color:orange;' class='glyphicon glyphicon-flag'></span><a style='color:orange;'>" . $warning . "</a><br>";
    }
}
  • 写回答

2条回答 默认 最新

  • duanjiongzhen2523 2015-09-07 07:06
    关注

    You need to use find()

    var warnings = $(data).find('#warnings');
    

    Note : Id should be unique , use class instead

    PHP:

    if (!empty($warning) && isset($warning))
    {
        foreach($warning as $warning)
        {
            echo "<span style='color:orange;' class='glyphicon glyphicon-flag warnings'></span><a style='color:orange;'>" . $warning . "</a><br>";
            //                                                                 --^-- adding class
        }
    }
    

    jQuery :

    posting.done(function(data){
        var warnings = $(data).find('.warnings');
    
        $('form#gastenboek').append(data);
    
        if (warnings.length > 0) {
        //           --^-- getting count of elements
            alert("warnings")
        } else {
            alert("Nothing");
        }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制