尝试使用jquery

时间:2015-08-21 17:55:55

标签: javascript jquery json

我从其他页面获取数据以填充输入,但我不知道如何使用该数据填充多选。这是我的多选:

<select multiple="multiple" class="multi-select" id="my_multi_select4" name="experience[]">
    <?php foreach(WorkModel::Work_List() as $work): ?>
         <option value="<?php echo System::escape($work->we_id); ?>"><?php echo System::escape($work->we_name); ?></option>
</select>

要填充它,我需要将选项与我的JSON中的以下数据分开:

例如:json_required_name

很多时候,不止一个(会使用foreach循环,但我的页面会在模式之前加载,从另一个页面获取数据)。

这是我的JSON示例:

   {"name":"Gev Offshore","description":"","duration":"1","country":"United Kingdom","employer":"gev offshort","start_date":"2015-08-01","job_start":"2015-08-21","job_end":"2015-08-21","requirements":[{"id":"1","name":"IRATA Level 1 Technician"},{"id":"2","name":"IRATA Level 2 Technician"},{"id":"3","name":"IRATA Level 3 Technician"},{"id":"4","name":"Equipment and Hardware Related"},{"id":"5","name":"NDT Aerospace"},{"id":"6","name":"NDT ECI"},{"id":"7","name":"NDT General"},{"id":"8","name":"NDT MPI"},{"id":"9","name":"NDT Radiography"},{"id":"10","name":"NDT Ultrasonic"},{"id":"11","name":"Rigging"},{"id":"12","name":"Rope Access"},{"id":"13","name":"Rope Access \/ NDT Management"}]}

2 个答案:

答案 0 :(得分:0)

这就是你要找的东西:

http://jsfiddle.net/1jjps0mv/

HTML:

<select multiple="multiple" class="multi-select" id="my_multi_select4" name="experience[]">
</select>

使用Javascript:

var xreturn = {"name":"Gev Offshore","description":"","duration":"1","country":"United Kingdom","employer":"gev offshort","start_date":"2015-08-01","job_start":"2015-08-21","job_end":"2015-08-21","requirements":[{"id":"1","name":"IRATA Level 1 Technician"},{"id":"2","name":"IRATA Level 2 Technician"},{"id":"3","name":"IRATA Level 3 Technician"},{"id":"4","name":"Equipment and Hardware Related"},{"id":"5","name":"NDT Aerospace"},{"id":"6","name":"NDT ECI"},{"id":"7","name":"NDT General"},{"id":"8","name":"NDT MPI"},{"id":"9","name":"NDT Radiography"},{"id":"10","name":"NDT Ultrasonic"},{"id":"11","name":"Rigging"},{"id":"12","name":"Rope Access"},{"id":"13","name":"Rope Access \/ NDT Management"}]};
$(function() {
    $.each(xreturn.requirements, function() {
        $('#my_multi_select4').append('<option value="'+this.id+'">'+this.name+'</option>');
    });
});

答案 1 :(得分:0)

你的WorkModel::WorkList()返回json还是返回一个关联数组?如果它返回后者,那么你的代码应该大致正确,并添加以下内容:

<select multiple="multiple" class="multi-select" id="my_multi_select4" name="experience[]">
<?php foreach(WorkModel::Work_List() as $work): ?>
     <option value="<?php echo System::escape($work->we_id); ?>"><?php echo System::escape($work->we_name); ?></option>
<?php endforeach; ?>
</select>

必须让endforeach;块知道何时终止循环,否则它将继续在select end标签上重复循环。

现在,如果前者是真的,那么你想首先将数据转换为关联数组,然后再使用json_decode执行循环:

<select multiple="multiple" class="multi-select" id="my_multi_select4" name="experience[]">
<?php foreach(json_decode(WorkModel::Work_List()) as $work): ?>
     <option value="<?php echo System::escape($work->we_id); ?>"><?php echo System::escape($work->we_name); ?></option>
<?php endforeach; ?>
</select>