简体   繁体   English

jQuery如何在我的表格中填写选择选项

[英]jquery how to fill select option in my form

enter image description here The json (data) looks like this guys! 在此处输入图片描述 json(数据)看起来像这样! I have a little form. 我有一个小表格。 Like this: 像这样:

<form method="post" action="" id="ajaxform">
<input type="text" size="32" maxlength="36" name="name" placeholder="Вaшe имя" val="">
        <select id="activity_2" name="activity_2">
        <option value="">Exmaple</option>
        </select>
<input type="submit" value="GO GO GO"/>
</form>
<script src="/add-information/aa.js"></script>

In this little code i receive a json with data from my database: 在这个小代码中,我从数据库中接收到包含数据的json:

        var data = JSON.parse(response);
        console.log(data);

in data there is id and name of the rubric. 在数据中,有编号和名称的名称。 How can i load all this array in my option list? 如何将所有此数组加载到我的选项列表中? I was told to do something like this: 有人告诉我要做这样的事情:

var select = document.getelementById('activity_2'); // id of the select 
select.options[i] = new Option(data.id, data.od); // add data?

help me please, how i can fill select with data from 'data'? 请帮助我,我怎样才能用“数据”中的数据填写选择内容?

THE SOLUTION BY ADEON IS: ADEON的解决方案是:

var data = JSON.parse(response); var data = JSON.parse(response); console.log(data); console.log(data);

    var select = document.getElementById('activity_2');

    for (var i = 0; i < data.data.length; i++) {
    select.options[select.length] = new Option(data.data[i].name_rus, data.data[i].id);
        }

You can loop your data and append option html to your select . 您可以循环data并将html选项附加到select

var select = document.getelementById('activity_2');
$(select).html('');
for (var i in data) {
    $(select).append('<option value=' + data[i] + '>' + data[i] + '</option>');
}

Assuming data is an array, you could iterate over it and apply it to the DOM like so. 假设data是一个数组,您可以对其进行迭代并将其应用于DOM。

var optionString = '';
var data = ['John', 'Josh'];

data.forEach(function(dataItem, index){
  optionString += '<option value="' + index + '">' + dataItem + '</option>';
});

$('#activity_2').append(optionString);

Here is a working jsfiddle: https://jsfiddle.net/9ke5fzqo/ 这是一个工作的jsfiddle: https ://jsfiddle.net/9ke5fzqo/

You should use add method which accepts as parameter a new Option . 您应该使用add方法,该方法接受一个新的Option作为参数。

The constructor of Option looks like this: new Option(text,value) . Option的构造函数如下所示: new Option(text,value)

 var array=[{ "text":"text1", "value":"value1" },{ "text":"text2", "value":"value2" }]; var select = document.getElementById('activity_2'); array.forEach(function(item){ select.add(new Option(item.text,item.value)); }); 
 <select id="activity_2" name="activity_2"> 

You can use $.append in jquery to add new option into a select input. 您可以在jquery中使用$ .append将新选项添加​​到选择输入中。 Html: HTML:

<select id="selinput" name="activity_2"> <option value="">Exmaple</option> </select>

Jquery: jQuery:

$("#selinput").append('<option value="1">value</option>');

Or you can use jquery.selec2 plugin. 或者,您可以使用jquery.selec2插件。

You need to do these: 您需要执行以下操作:
1) Your call getelementById should be changed to getElementById otherwise you would receive error. 1)您的调用getelementById应该更改为getElementById否则您将收到错误。
2) You need to create options string first and then append that string to DOM rather than touching DOM every time you add options. 2)您需要先创建选项字符串,然后将该字符串附加到DOM,而不是每次添加选项时都接触DOM。 Touching DOM as less as possible is a good idea from performance point of view. 从性能的角度来看,尽可能少地接触DOM是一个好主意。
3) To simplify the syntax you can use string interpolation syntax like below: 3)为了简化语法,您可以使用如下所示的字符串插值语法:

 var select = document.getElementById('activity_2'); //$(select).html(''); var data = [{id:1, name_rom:'a', name_rus:'abc'},{id:2, name_rom:'x', name_rus:'xyz'}]; var options = ""; for (var i = 0; i < data.length; i++) { options += `<option value=${data[i].id}>${data[i].name_rus}</option>`;//<--string //interpolation } $(select).append(options); //OR below, so you don't need to call document.getElementById //$('#activity_2').append(options); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form method="post" action="" id="ajaxform"> <input type="text" size="32" maxlength="36" name="name" placeholder="Вaшe имя" val=""> <select id="activity_2" name="activity_2"> <option value="">Exmaple</option> </select> 

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

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