我想分割一个字符串,想把下拉列表在html, javascript

I want to split a string and want to put in the dropdown list in html, javascript

本文关键字:下拉列表 html javascript 字符串 一个 分割      更新时间:2023-09-26
strPropertyEvents=20 Aug 2014-New Activity 1 21 Aug 2014-gfdbfjdb 21 Aug 2014-anubhav 24 ug 2014-hjdf

在strPropertyEventas中它是以字符串的形式存储的但是我想把这个字符串分割成这样的方式输出在一个下拉列表中,如

20 Aug 2014-New Activity 1
21 Aug 2014-gfdbfjdb 
21 Aug 2014-anubhav
24 ug 2014-hjdf

请告诉我代码,以便我可以使用

使用分隔符,或者像mritunjay告诉splice的那样,但是slice对于长字符串会有问题。这是你创建的字符串还是从远程获取的字符串?如果您可以控制字符串strPropertyEvents,那么添加分隔符,如jsbin_example

那么你可以这样做

<select id="list">
</select>
var strPropertyEvents='20 Aug 2014-New Activity 1###21 Aug 2014-gfdbfjdb###21 Aug 2014-anubhav###24 ug 2014-hjdf';
var strList = strPropertyEvents.split('###');
console.log(strList);
$.each(strList,function(index,val){
 $("#list").append('<option>'+val+'</option>');
});