从Jquery post返回中选择元素

Select element from Jquery post return

本文关键字:选择 元素 返回 Jquery post      更新时间:2023-09-26

我试图从jquery返回数据后选择一个元素。字符串返回,但当我尝试从返回html字符串中选择一个元素时,选择总是空的。这是一个正在加载greasemonkey的用户脚本。

refresh();
function refresh(___id,___action,___page){
            $.post("http://www.example.com", {auction_id:___id,action:___action,page:___page}, function(bidpage){
            var auction_id = $("#auction_id", bidpage).val();
            console.log(auction_id);
});

这篇文章返回这个数据

    <html><head></head><body><input type="hidden" name="auction_id" id="auction_id" value="8583949"></body></html>

auction_id总是null。我找不到任何解决办法。谢谢你的帮助。

这是因为您将bidpage作为字符串。您需要首先将其转换为HTML:

var auction_id = $("#auction_id", $( bidpage ) ).val();

根据jQuery API [context]应该是DOM Element, Document或jQuery。

评论:

似乎jQuery剥离<html><body>元素,所以你需要使用:

var auction_id = $( bidpage ).val();

您可以尝试将您的post数据添加到div:

<div id="appendpostdatahere"></div>

function refresh(___id,___action,___page){
  $.post("http://www.example.com", {auction_id:___id,action:___action,page:___page}, 
    function(bidpage){
      $('#appendpostdatahere').html(bidpage);
      var auction_id = $('#auction_id).val();
      console.log(auction_id);
    });
}