我正在使用聊天机器人,情况令人担忧。如果找到多个匹配结果,则显示以下多个结果。我想正确格式化它,我想在结果之前显示数字。如何使用javascript。
function createRowMultiresult(jobjects) {
var $div = $('<div class="chat Bot"></div>');
var $div2 = $('<div class="user-photo"><img src="{% static
"Robot.jpg"%}" /></div>');
$div.append($div2);
var $tbl = $('<table style="width:100%;"></table>');
if (jobjects.length>1){
var $tr = $('<tr><td style="padding:5px;">Multiple results Found for
your query. Please search with specific keyword</td></tr>');
$tbl.append($tr);
}
for(var x=0; x<jobjects.length;x++){
var currentobj = jobjects[x];
if (currentobj.ans.indexOf("/AV") != -1){
var $tr = $('<tr><td style="padding:5px;"><a href="https:GUIDES'
+ currentobj.ans +'" target="_blank" >Click Here for
Download</a></td></tr>');
$tbl.append($tr);
}
else{
var $tr = $('<tr><td style="padding:5px;">' +
currentobj.ans.replace(/[^\w\s]/gi, "<br/>") +'</td>
</tr>');
$tbl.append($tr);
if (currentobj.Pic.length>7){
var $tr = $("<tr><td style='text-align:center;'><img
class='productpic' src='{% static '/Pictures/' %}"
+ currentobj.Pic +"' /></td></tr>");
$tbl.append($tr);
}
}
}
var $par = $('<p class="chat-message"></p>');
$par.append($tbl);
$div.append($par);
$chatlog.append($div);
}
Actual output:
Multiple result found-
car-finance loan1
car-finance loan2
car-finance loan3
car-finance loan4
Expected output-
1.car-finance loan1
2.car-finance loan2
3.car-finance loan3
4.car-finance loan4
答案 0 :(得分:1)
您已经有了for(var x=0; x<jobjects.length;x++){
的编号
只要在需要订购号的地方添加x值即可。
像下面一样
function createRowMultiresult(jobjects) {
var $div = $('<div class="chat Bot"></div>');
var $div2 = $('<div class="user-photo"><img src="{% static
"Robot.jpg" %
}
" /></div>');
$div.append($div2);
var $tbl = $('<table style="width:100%;"></table>');
if (jobjects.length > 1) {
var $tr = $('<tr><td style="padding:5px;">Multiple results Found
for your query.Please search with specific keyword < /td></tr > ');
$tbl.append($tr);
}
for (var x = 0; x < jobjects.length; x++) {
var currentobj = jobjects[x];
if (currentobj.ans.indexOf("/AV") != -1) {
var $tr = $('<tr><td style="padding:5px;"><a href="https:' +
currentobj.ans + '" target="_blank" >Click Here</a> <
/td></tr > ');
$tbl.append($tr);
}
else {
var $tr = $('<tr><td
style = "padding:5px;" > 'x + ' ' + currentobj.ans.replace(/[^\w\s]/gi, "<br/>") +
'</td></tr>');
$tbl.append($tr);
if (currentobj.Pic.length > 7) {
var $tr = $("<tr><td style='text-align:center;'><img class='productpic'
src = '{% static ' / Pictures / ' %}" + currentobj.Pic +"' / > < /td></tr > ");
$tbl.append($tr);
}
}
}
var $par = $('<p class="chat-message"></p>');
$par.append($tbl);
$div.append($par);
$chatlog.append($div);
}
让我知道这是否有帮助。
答案 1 :(得分:0)
您的代码几乎是正确的,您只需要一点点修改就可以更改
for(var x=0; x<jobjects.length;x++) {
var currentobj = jobjects[x];
if (currentobj.ans.indexOf("/AV") != -1){
var $tr = $('<tr><td style="padding:5px;"><a href="https:' +
currentobj.ans +'" target="_blank" >Click Here</a>
</td></tr>');
$tbl.append($tr);
}
else {
var $tr = $('<tr><td
style="padding:5px;">'currentobj.ans.replace(/[^\w\s]/gi, "<br/>")
+'</td></tr>');
$tbl.append($tr);
if (currentobj.Pic.length>7){
var $tr = $("<tr><td style='text-align:center;'><img class='productpic'
src='{% static '/Pictures/' %}" + currentobj.Pic +"' /></td></tr>");
$tbl.append($tr);
}
}
}
上面的代码需要用下面的代码更新
for(var x=0; x<jobjects.length;x++){
var currentobj = jobjects[x];
if (currentobj.ans.indexOf("/AV") != -1){
var $tr = $('<tr><td style="padding:5px;"><a href="https:' +
currentobj.ans +'" target="_blank" >Click Here</a>
</td></tr>');
$tbl.append($tr);
}
else {
var $tr = $('<tr><td
style="padding:5px;">'Number(x + 1)' + ' ' +'currentobj.ans.replace(/[^\w\s]/gi, "<br/>")
+'</td></tr>');
$tbl.append($tr);
if (currentobj.Pic.length>7){
var $tr = $("<tr><td style='text-align:center;'><img class='productpic'
src='{% static '/Pictures/' %}" + currentobj.Pic +"' /></td></tr>");
$tbl.append($tr);
}
}
}
答案 2 :(得分:0)
在这里,
function createRowMultiresult(jobjects) {
var $div = $('<div class="chat Bot"></div>');
var $div2 = $('<div class="user-photo"><img src="{% static
"Robot.jpg"%}" /></div>');
$div.append($div2);
var $tbl = $('<table style="width:100%;"></table>');
if (jobjects.length>1){
var $tr = $('<tr><td style="padding:5px;">Multiple results Found for
your query. Please search with specific keyword</td></tr>');
$tbl.append($tr);
}
for(var x=0; x<jobjects.length;x++){
var currentobj = jobjects[x];
if (currentobj.ans.indexOf("/AV") != -1){
var $tr = $('<tr><td style="padding:5px;"><a href="https:GUIDES'
+ currentobj.ans +'" target="_blank" >Click Here for
Download</a></td></tr>');
$tbl.append($tr);
}
else{
var $tr = $('<tr><td style="padding:5px;">' +(x+1)+'.'+
currentobj.ans.replace(/[^\w\s]/gi, "<br/>") +'</td>
</tr>');
$tbl.append($tr);
if (currentobj.Pic.length>7){
var $tr = $("<tr><td style='text-align:center;'><img
class='productpic' src='{% static '/Pictures/' %}"
+ currentobj.Pic +"' /></td></tr>");
$tbl.append($tr);
}
}
}
var $par = $('<p class="chat-message"></p>');
$par.append($tbl);
$div.append($par);
$chatlog.append($div);
}