为什么我的警报框不显示正确的文本?

问题描述:

我点击一个按钮后显示一个警告框,但字符串文本正在关闭。为什么我的警报框不显示正确的文本?

这是我的代码:

jQuery('input[name="addtocart"]').each(function() 
{ 
    jQuery(this).on("click", function(event) 
    { 
     qty = jQuery(this).parent().find('input[name="qty"]').val(); 
     var qty1 = parseFloat(qty); 
     qth = jQuery(this).parent().parent().find('.qtyonhand').text(); 
     var qth1 = parseFloat(qth); 
     itemName = jQuery(this).parent().parent().parent().find('.cell-desc   
     span').text(); 
     if(qty1 > qth1){ 
      alert("For the following item, the ordered quantity exceeds the 
      current available quantity.Please adjust the quantity and 
      retry.\n"+itemName+"-"+"Available Qty:"+qth1); 
     } 
    }); 
}); 

这里是图像:

Alert Message

在上述图像,可用性文本显示在下一行。 如何在单行显示字符串?请帮忙。

+0

使用'... + itemName.trim() +“ - ”+“可用...' – adeneo

+0

感谢它的工作正常 –

看来你需要用空格替换换行符。 的if语句之前添加以下代码:

itemName = itemName.replace(/\n/g, " "); 

正则表达式来自this thread

if(qty1 > qth1){ 
    alert("For the following item, the ordered quantity exceeds the 
    current available quantity.Please adjust the quantity and  
    retry."+itemName+"-"+"Available Qty:"+qth1); 
} 

请从警报的内容中删除\ n