简体   繁体   English

从用逗号分隔的html内容填充选择框

[英]Populating a selectbox from html content split by commas

I am writing a code for a form with a select box in it that has to get populated by the content of a specific html part. 我正在为其中带有选择框的表单编写代码,该表单必须由特定html部件的内容填充。 I managed to get an id on the specific part (it gets created automatically by the cms) so you can call up the part with javascript. 我设法在特定部分上获取了一个ID(该ID由cms自动创建),因此您可以使用javascript调用该部分。

Problem is i cant figure out how to get the content split by commas like "red, blue, yellow" and split it up into a select box as options. 问题是我无法弄清楚如何用逗号分隔内容,例如“红色,蓝色,黄色”,并将其拆分为一个选择框作为选项。

Here is the part of the javascript code that i have: 这是我拥有的javascript代码的一部分:

    function offerte(){
    var kleuren = document.getElementById('product_kleuren'); 

// here has to come a code to populate the select box
    }

and the html part: 和html部分:

    <div><span class="extra_fields_name">Kleuren</span>:
    <span id="product_kleuren" class="extra_fields_value">Naturel, Helder, Zwart</span></div>

    <label class="offerte_lbl">Kleur: </label><select id='kleuren'  class='offerte_input'></select><br>

I know its not much, I'm still learning javascript but i really cant figure out how to get the content of a specific html part that is split by commas and seperate them into values to populate the selectbox with. 我知道的不多,我仍在学习javascript,但我真的想不通如何获取用逗号分割的特定html部分的内容,并将其分隔成值以填充selectbox。

Try 尝试

var kleuren = $('#product_kleuren').text(); 
var splitter = kleuren.split(',');
var options = '';
for(var i = 0; i < splitter.length; i++)
{

 options += '<option value="' + splitter[i] + '">' + splitter[i]+  '</option>';
}
$('#kleuren').html(options);

Use this code using jquery.. 通过jquery使用此代码。

var arr = $("#product_kleuren").split(",");

for(var i=0;i<arr.length;i++)
$("#kleuren").append("<option>"+arr[i]+"</option>");

Using Pure JS, you can use .split(",") to put the split values in an array, then iterate through the array creating option elements: 使用Pure JS,可以使用.split(",")将拆分值放入数组中,然后遍历数组以创建option元素:

<select id="someSelect"></select>

JS JS

function offerte(){
    var kleuren = document.getElementById('product_kleuren');
    var parts = kleuren.innerHTML.split(",");

    for (var i = 0; i < parts.length; i++) {
        var option = document.createElement("option");
        option.text = parts[i];
        option.value = i;
        document.getElementById("someSelect").appendChild(option);
    }
}

Demo: http://jsfiddle.net/fvDPh/ 演示: http//jsfiddle.net/fvDPh/

You can split a string with String.split : 您可以使用String.split分割字符串:

//split the words into an array called "words"
var words = kleuren.split(',');

//iterate through the words with jQuery.each()
$(words).each(function() {
    word = this.trim();   //trim any extra whitespace from the word

    //now you can add the word as an option to the <select>
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM