Вопрос

I have some pages that i acces by jquery $.post like this:

$.post(url, {name: name}, function(data)
{
    var htmldata = $(data);
    if($('#ok', htmldata).val() == "1")
    {
        //some things carried out
    }
});

$('#ok', htmldata).val() is allways undefined so where is the problem?

UPDATE: htmldata = <input id=\"ok\" type=\"hidden\" value=\"1\" /> and another 2-3 hidden input.

Это было полезно?

Решение

$('#ok', htmldata) will look for all descendants of htmldata that have the id of ok.

You need to change $('#ok', htmldata) to htmldata.filter('#ok')

Другие советы

Actually no need to use $ in the following line:

var htmldata = $(data);

Use directly var htmldata = data; and try to check whether any ata is there in htmldata.If yes then proceed further.

You can use find():

$.post(url, {name: name}, function(data)
{
    var htmldata = $(data);
    if(htmldata.find('#ok').val() == "1")
    {
        //some things carried out
    }
});

Because data in htmldata is not initialized in DOM, if you want that work inject this into DOM in hidden div for example.

But better approach is return JSON from AJAX and then check for status is very easy.

Of Course you can always parse using regex :)

Example of first case (not tested) - put this into callback function:

$("#ajaxData").remove();
$('body').append('<div id="ajaxData">' + htmldata + '</div>');
if(parseInt($('#ok').val()) == "1")
{

}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top