JQuery если предложение - PullRequest
       14

JQuery если предложение

0 голосов
/ 15 сентября 2009

Мне нужна помощь, чтобы написать оператор if, используя jquery. Поэтому, если result.d.ProductName пусто, не показывать `$ (prdHtml) .html (html);

$.ajax({
    type: "POST",
    url: "Services.asmx/GetProduct",
    data: '{ "fieldName": "' + id + '"}',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
success: function(results) {
        var html = '<h3>' + results.d.ProductName + '<h3>'
                    + '<a href=""' + results.d.Url + '</a>';
        $(prdHtml).html(html);

Ответы [ 3 ]

4 голосов
/ 15 сентября 2009
success: function(results) {
        if(results.d.ProductName.length) {
            var html = '<h3>' + results.d.ProductName + '<h3>'
                        + '<a href=""' + results.d.Url + '</a>';

                $(prdHtml).html(html);
        } else {
            $(prdHtml).hide();
        }
}
1 голос
/ 15 сентября 2009

попробуй:

success: function(results) {
if (results.d.ProductName!="") {
        var html = '<h3>' + results.d.ProductName + '<h3>'
                    + '<a href=""' + results.d.Url + '</a>';
        $(prdHtml).html(html);
}
}
0 голосов
/ 15 сентября 2009
success: function(results) {
  // should probably test if d exists before testing for productname
  if (typeof(results.d) == "undefined" || typeof(results.d.ProductName ) != "string")
   return;

  ...code
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...