[英]For loop <p> tag in jquery
I need to loop using jQuery. 我需要循环使用jQuery。 Sometimes my query returns more than one row, so I want to check if the query returned more than one. 有时我的查询返回的行多,所以我想检查查询是否返回多行。 I want to view all outputs in p
tags (one by one). 我想查看p
标签中的所有输出(一个接一个)。 I wrote my code and it gives only the last row in my query. 我写了代码,它只给出查询的最后一行。 How can I return all rows in <p>
tags? 如何返回<p>
标记中的所有行? What is the error in my code? 我的代码有什么错误?
<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");
}
}
}
}
}
);
});
Let's analyze them :-) 让我们分析一下它们:-)
You're using a condition when the array jdata
's length === 1 within the for-loop
. 您使用的条件是for-loop
数组jdata
的长度=== 1。 Move that logic outside from the for-loop
. 将该逻辑从for-loop
移出。
for (var x = 0; x < jdata.length; x++) {
if (1 < jdata.length) {
^
You're overriding the content of your previously call of function .html(...)
. 您将覆盖先前调用的函数.html(...)
。 Therefore, you're losing the previous HTML. 因此,您将丢失以前的HTML。
$("#prodDeccarea").html("Your product catagory is " + td1 + ".");
^
You're stringifying an already JSON String, just parse it. 您正在对一个已经为JSON的字符串进行字符串化,只需对其进行解析即可。
var jString = JSON.stringify(result);
^
You have multiple conditions to set a value according to tdr3's value. 您有多个条件可以根据tdr3的值设置一个值。 Just use that value and set it to the element. 只需使用该值并将其设置为元素即可。 Ie: $("#slsiUnit").val("UNIT" + td3);
即: $("#slsiUnit").val("UNIT" + td3);
if (td3 == "1") { <- Here
$("#slsiUnit").val("UNIT1");
}
if (td3 == "2") { <- Here, and so on
$("#slsiUnit").val("UNIT2");
}
$("#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>"); } } }); });
You can use each method https://api.jquery.com/each/ 您可以使用每种方法https://api.jquery.com/each/
$("p").each(function(element) {
console.log(element.text);
});
This will print out the contents of all p elements in your code. 这将打印出代码中所有p元素的内容。
$.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
});
}
});
guess the problem is here,you didn't accumulate; 猜猜问题出在这里,你没有积累;
$("#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");
}
}
}
}
}
);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.