对于jquery中的循环<p>标记

时间:2018-02-04 11:03:14

标签: javascript jquery html ajax loops

我需要使用jQuery循环。有时我的查询返回多行,所以我想检查查询是否返回多个。我想查看p标签中的所有输出(逐个)。我编写了我的代码,它只给出了查询中的最后一行。如何返回<p>代码中的所有行?我的代码中有什么错误?

 <p id="prodDeccarea"> </p> 
$("#slsNo").keyup(function () {
    $("#hsCode").val(null);
    $("#slsiUnit").val(null);
    var slsNo = $("#slsNo").val();
    $.ajax({
        type: 'GET',
        url: '${pageContext.request.contextPath}/restservice/ViewProd/' + slsNo,
        success: function (result) {
            var jString = JSON.stringify(result);
            var jdata = JSON.parse(jString);
            for (var x = 0; x < jdata.length; x++) {
                if (1 < jdata.length) {
                    var td1 = jdata[x].itemDesc;
                    var td2 = jdata[x].hsCode;
                    var td3 = jdata[x].slsiUnit;
                    $("#prodDeccarea").html("Your product catagory is " + jdata[x].itemDesc + ".");

                } else {
                    var td1 = jdata[x].itemDesc;
                    var td2 = jdata[x].hsCode;
                    var td3 = jdata[x].slsiUnit;
                    $("#hsCode").val(td2);
                    $("#prodDeccarea").html("Your product catagory is " + td1 + ".");
                    if (td3 == "1") {
                        $("#slsiUnit").val("UNIT1");
                    }
                    if (td3 == "2") {
                        $("#slsiUnit").val("UNIT2");
                    }
                    if (td3 == "3") {
                        $("#slsiUnit").val("UNIT3");
                    }
                    if (td3 == "4") {
                        $("#slsiUnit").val("UNIT4");
                    }
                    if (td3 == "5") {
                        $("#slsiUnit").val("UNIT5");
                    }
                    if (td3 == "6") {
                        $("#slsiUnit").val("UNIT6");
                    }
                }
            }
        }
    }
    );
});

4 个答案:

答案 0 :(得分:2)

您的代码中存在一些问题

让我们分析一下: - )

jdata内的数组for-loop的长度=== 1时,您正在使用条件。将该逻辑移出for-loop

for (var x = 0; x < jdata.length; x++) {
    if (1 < jdata.length) { 
    ^

您已覆盖之前调用的函数.html(...)的内容。因此,您将丢失以前的HTML。

$("#prodDeccarea").html("Your product catagory is " + td1 + ".");
                       ^

您正在对已经存在的JSON字符串进行字符串化,只需解析它。

var jString = JSON.stringify(result);
                   ^

您有多个条件可根据tdr3的值设置值。只需使用该值并将其设置为元素即可。我:$("#slsiUnit").val("UNIT" + td3);

if (td3 == "1") { <- Here
    $("#slsiUnit").val("UNIT1");
} 
if (td3 == "2") { <- Here, and so on
    $("#slsiUnit").val("UNIT2");
}

请查看包含这些修补程序的代码段

&#13;
&#13;
$("#slsNo").keyup(function() {
  $("#hsCode").val(null);
  $("#slsiUnit").val(null);
  var slsNo = $("#slsNo").val();
  $.ajax({
    type: 'GET',
    url: '${pageContext.request.contextPath}/restservice/ViewProd/' + slsNo,
    success: function(result) {
      var jString = JSON.stringify(result);
      var jdata = JSON.parse(jString);

      if (jdata.length === 1) {
        var td1 = jdata[0].itemDesc;
        var td2 = jdata[0].hsCode;
        var td3 = jdata[0].slsiUnit;
        $("#hsCode").val(td2);
        $("#prodDeccarea").html("Your product catagory is " + td1 + ".");
        $("#slsiUnit").val("UNIT" + td3);
        
        return;
      }

      for (var x = 0; x < jdata.length; x++) {
        var td1 = jdata[x].itemDesc;
        var td2 = jdata[x].hsCode;
        var td3 = jdata[x].slsiUnit;
        var $prodDeccarea = $("#prodDeccarea");
        $prodDeccarea.html($prodDeccarea.html() + '<p>' + "Your product catagory is " + jdata[x].itemDesc + "." + "</p>");
      }
    }
  });
});
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您可以使用每种方法https://api.jquery.com/each/

$("p").each(function(element) {
  console.log(element.text);
});

这将打印出代码中所有p元素的内容。

答案 2 :(得分:1)

$.ajax({
    type: 'GET',
    url: '${pageContext.request.contextPath}/restservice/ViewProd/' + slsNo,
     dataType: "json",//if you return json data you don't need var jString = JSON.stringify(result);var jdata = JSON.parse(jString);
     beforeSend: function () {
       $("#prodDeccarea").html("");//this will clear previous results 
     },
    success: function (data) {
        $.each(data, function (i, e) {//this will loop through all the results
            $("#prodDeccarea").append("<p>RESULT " + i + " : ".JSON.stringify(e));//this will add to the at the end of #prodDeccarea
        });
    }
});

答案 3 :(得分:0)

猜测问题就在这里,你没有积累;

    $("#slsNo").keyup(function () {
        $("#hsCode").val(null);
        $("#slsiUnit").val(null);
        var slsNo = $("#slsNo").val();
        $.ajax({
            type: 'GET',
            url: '${pageContext.request.contextPath}/restservice/ViewProd/' + slsNo,
            success: function (result) {
                var jString = JSON.stringify(result);
                var jdata = JSON.parse(jString);
                for (var x = 0; x < jdata.length; x++) {
                    if (1 < jdata.length) {
                        var td1 = jdata[x].itemDesc;
                        var td2 = jdata[x].hsCode;
                        var td3 = jdata[x].slsiUnit;
                        //accumulate
                        $("#prodDeccarea").html($("#prodDeccarea").html()+"Your product catagory is " + jdata[x].itemDesc + ".");


                    } else {
                        var td1 = jdata[x].itemDesc;
                        var td2 = jdata[x].hsCode;
                        var td3 = jdata[x].slsiUnit;
                        $("#hsCode").val(td2);
                        $("#prodDeccarea").html("Your product catagory is " + td1 + ".");
                        if (td3 == "1") {
                            $("#slsiUnit").val("UNIT1");
                        }
                        if (td3 == "2") {
                            $("#slsiUnit").val("UNIT2");
                        }
                        if (td3 == "3") {
                            $("#slsiUnit").val("UNIT3");
                        }
                        if (td3 == "4") {
                            $("#slsiUnit").val("UNIT4");
                        }
                        if (td3 == "5") {
                            $("#slsiUnit").val("UNIT5");
                        }
                        if (td3 == "6") {
                            $("#slsiUnit").val("UNIT6");
                        }
                    }
                }
            }
        }
        );
    });