javascript 在对话框 <div> 内的消息中添加新行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6515671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 20:55:45  来源:igfitidea点击:

add new line in my message inside a dialog <div>

javascriptjqueryhtml

提问by jayAnn

just wanted to ask how to put message in another line after the other. Here's my code in my program:

只是想问问如何将消息依次放在另一行中。这是我的程序中的代码:

function isEmpty(field,name,minVal, maxVal){
  var msg ="";
  if (field.val() >= minVal || field.val() <= maxVal){
    if ( field.val().length == 0){ 
      msg=(name + ' is empty.\n');
    }
  }
  else{
    msg=(name + ' inputted is invalid or number is more than 1000. \n');
  }  
  return msg;
}

function validateTallyReq(par){
  var msg= "Please read the following:";

  msg += isEmpty($('#tallyPlankNo'),'Plank Number ',1,999) ;        
  msg += isEmpty($("#tallyThick"), 'Thickness value',.9,999);
  msg += isEmpty($("#tallyWidth"), 'Width Value',.9,999);
  msg += isEmpty($("#tallyLength"), 'Lenght Value',.9,999);
  msg += isEmpty($("#tallyQty"), 'Quantity',1,3);

  if (msg == "")  { 

  } else {
    showMessage(msg);    
  }
  return false; 
}

this outputs message in one line. What I want is the message to output like:

这在一行中输出消息。我想要的是输出如下消息:

Please read the following:

Plank Number is empty. 
Thickness value is empty. 
Width Value is empty. 
Lenght Value inputted is invalid or number is more than 1000. 
Quantity is empty. 

And one more question, I really dont know why it shows the length value is invalid though i dont input any value on it,. Please help.

还有一个问题,我真的不知道为什么它显示长度值无效,尽管我没有在上面输入任何值。请帮忙。

EDIT
This is my showMessage function:

编辑
这是我的 showMessage 函数:

function showMessage(msg) {
  $('#dialog #message').text(msg);
  $('#dialog').dialog('open');
}

回答by bbg

Try adding a <br/>to the message after each line. That will work if the the show()method is doing something like $("#displaydiv").html(msg);

尝试<br/>在每行之后的消息中添加一个。如果该show()方法正在执行类似的操作,那将起作用$("#displaydiv").html(msg);

msg += "<br/>";

As for the unexpected invalidmessage, it looks like the logic in this conditional is faulty:

至于意外的invalid消息,这个条件中的逻辑看起来是错误的:

if (field.val() >= minVal || field.val() <= maxVal)

Try making it

尝试制作

if (field.val() >= minVal && field.val() <= maxVal)

回答by AUSteve

You need to replace the \nwith <br/>since you are displaying via HTML.

由于您是通过 HTML 显示的\n,因此您需要将 替换为<br/>

回答by Percy

Something like this might fix both of your

像这样的事情可能会解决你的

function isEmpty(field,name,minVal,maxVal) {
    var msg ="";
    if (field.val().length == 0) { 
        msg = name + ' is empty.<br/>';
    } else if (field.val() < minVal || field.val() > maxVal) {
        msg = name + ' must be between ' + minVal + ' and ' + maxVal + '.<br/>';
    }  
    return msg;
}