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

标签 javascript jquery html

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

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; 
}

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

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. 

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

编辑
这是我的 showMessage 函数:

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

最佳答案

尝试添加 <br/>每行后的消息。如果 show() 会起作用方法正在做类似 $("#displaydiv").html(msg); 的事情

msg += "<br/>";

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

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

试试看

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

关于javascript - 在对话框 <div> 中的消息中添加新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6515671/

相关文章:

javascript - 防止在 iOS 设备上缩放时出现锯齿 SVG 元素

javascript - 当用户单击鼠标右键时,如何阻止浏览器打开对话框?

javascript - document.getElementByID 外部还是内联?

ASP.NET MVC2 JsonResult 此请求已被阻止

html - 是否添加位置 : absolute to a block element make it behave like an inline?

javascript - 使用 D3 创建一个表,其中两行是兄弟

javascript - 沿 d3 中的路径拖动

javascript - JS/jQuery/Canvas - 数组平铺 map ,在行和列中查找数组的值

javascript - 如何创建 TreeMap WebGL

javascript - 如何获取多个输入作为数组或对象?