doz95923 2012-08-08 06:31
浏览 14
已采纳

如何访问livequery检测到的元素的值

I am using $_SESSION to pass back a value in the event of page redirect.

As jquery cannot access $_SESSION I decided to use PHP to output the value to a hidden div and remove that div once the value had been picked up. I expect that there is a neater way to do this but I don't know how else to expose the $_SESSION variable to jquery.

<?php       
    $pass_Back = $session->get_Pass_Back();
    $session->clear_Pass_Back();
?>

<?php
    if (count($pass_Back) != 0){
        echo "<input class=\"field_Input_Left\" id=\"schedule_Description\" type=\"text\" name=\"schedule_Description\" value=\"" . array_shift($pass_Back) . "\"/><br />";
        echo "<div id=\"pass_Back\" style=\"visibilty: hidden;\" ></div>";
    } else {    
        echo "<input class=\"field_Input_Left\" id=\"schedule_Description\" type=\"text\" name=\"schedule_Description\"/><br />";
    }
?>

Once this was done I needed a method to let jquery know if and when this new element was added to the DOM. Hence I used the plugin livequery to match when the element added. This it does which is great.

But when I try to access the value in the div it states that it is undefined.

$("#pass_Back").livequery(function(){
    if ($("#class_Name").val() != 0){
        var class_Name = $("#class_Name").val();
        get_Schedule_Data(class_Name);
    } else {
        var class_Name = "ALL";
        get_Schedule_Data(class_Name);
    }   


    $value = $(this).attr("value");
    auto_Fill_Schedule($("#pass_Back").attr("value"));

    // destroy pass back

    $("#pass_Back").remove();
});

When reviewed in firebug I note that at the point that livequery finds the added element no html is displayed. The DOM is ready otherwise the livequery couldn't have functioned but is this why no value is found?

Any guideance gratefully received.

  • 写回答

2条回答 默认 最新

  • duanfu3884 2012-08-08 06:35
    关注

    Don't use this:

    $value = $(this).attr("value");
    

    Do the following to get a value (for inputs in most of cases):

    (assuming $(this) is your input)

    $value = $(this).val();
    

    For div cases, there is no value, but you can get the html or text from inside as the value:

    (assuming $(this) is your div)

    $value = $(this).html();
    //or:
    $value = $(this).text();
    

    Just to know...

    You can mix PHP with jQuery but take a look at my answer from this post for better understanding:

    Is echoing Javascript code condtionally based on server-side logic considered harmful?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)