jQuery设置html()值 [英] jQuery set html() value
问题描述
HTML
<select class="selectAddress" name="select2" size="4" multiple="multiple">
<option>address 1</option>
<option>address 2</option>
<option>address3, some city, uk</option>
<option>address4, some city, uk</option>
<option>address4, some city, uk</option>
</select>
<p id="chosenAddress01" class="renderedYellowBox">result in here</p>
jQuery
$(".selectAddress").dblclick(function() {
var address = [];
$('.selectAddress option:selected').each(function(i, selected){
address[i] = $(selected).text();
});
//alert(address);
$('#chosenAddress01').html(address);
});
问题
我正在尝试获取地址选项的选定值以填充地址的dblclick()上的p标记
I'm trying to get the selected value of the address option to populate the p tag on dblclick() of the address
如果我使用警告框来检查结果,正确的结果通过。但是尝试将结果输入p标签不会返回任何内容。
If I use the alert box to check the result, the correct result comes thru. But trying to get the result into the p tag returns nothing.
任何人都可以帮忙吗?
谢谢,
Kevin
Thanks, Kevin
推荐答案
使用 .join()
将其首先变为字符串,如下所示:
Use .join()
to turn it into a string first, like this:
$('#chosenAddress01').html(address.join(', '));
.html()
以不同的方式处理数组,因此最好明确地将其作为字符串,因为这就是你所追求的。 alert()
的原因是有一个隐含的 .toString()
在那里。
.html()
treats an array differently, so best to explicitly make it a string since that's what you're after. The reason alert()
works is there's an implicit .toString()
going on there.
这篇关于jQuery设置html()值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!