开发者

How to separate data received from $.get() callback function

I have this:

开发者_运维百科<div ><span id="compareResult"></span>&nbsp;&nbsp;<span id="result"></span></div>


$.get(
      'data.php', 
      { valA: $(this,'option:selected').val() , valB:$(this,'option:selected').val()},
      function(childData){
            $('#result').html(childData).css('display', 'none')});>

PHP:

function getData($names,$id,$flag){
    if($flag==0){
    $strResult = '<select multiple class="sel" id="selectname" size='.count($names).'>';
    }
    if($flag==1){
    $strResult = '<select multiple class="sel" id="selname" size='.count($names).'>';
    }

    for($i=0; $i<count($names); $i++)
    {
        $strResult.= '<option value='.$id[$i].'>'.$names[$i].'</option>';
    }
    echo $strResult.= '</select>';}

How do I break apart/parse the response inside .html(childData) so I can put it in different span(i.e. id="compareResult")?

Thanks in advance.


This doesn't directly answer your question, but it answers what I think the real question might be.

If the data is coming from your own server, I strongly recommend you look at the jQuery Taconite plugin. It makes child's play of doing multiple-element updates via AJAX. All you have to do is make a slight change to the format of how the data is delivered to the browser.

Seriously, check it out.


After you make the html call to update #result's HTML, you will be able to access anything that was inserted inside #result in subsequent calls.

For example:

$.get(
      'data.php', 
      { valA: $(this,'option:selected').val() , valB:$(this,'option:selected').val()},
      function(childData){
            $('#result').html(childData).css('display', 'none');
            $('#result #someContainer').appendTo('#compareResult');
      }
);


I responded to a similar question recently, but you can actually grab any information you might need from the HTML response by creating a jQuery object of the response. For example:

var response = $("<div>This is my element <span class='a-thing'>Thing</span></div>");
alert(response.find('.a-thing').text());

In your callback, you could do the same thing:

$.get(
    'data.php', 
    { valA: $(this,'option:selected').val() , valB:$(this,'option:selected').val()},
    function(childData){
        var result = $(childData);
        alert(result.find('.some-selector').text());
    }
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜