我有一个:
$('#product_category > option:selected').each(function()
并将其附加到某个框的
.append(<div>'+$(this).text()+'</div>);
但我想将文本替换为仅显示 9 个字符。如果长度大于 9,则写入前 9 和 +"..."
我尝试过:
$('#product_category > option:selected').each(function() {
var text = $(this).text();
if(text.val().length > 8)
{
text = text.substr(0,9)+"...";
}
$('#confirm-category').append('<div class="alert alert-success">'+text+'</div>');
});
但它不起作用:/
我很乐意提供任何帮助:)
最佳答案
改变你的if条件
if(text.val().length > 8)
至
if(text.length > 8)
你不需要val()
,因为你已经有了它的文本text()
。
var text = $(this).text(); //<---here
关于javascript - jQuery substr() 多选值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18824295/