如何通过jsonencode数据使用Javascript选择下拉值

时间:2016-01-13 05:49:23

标签: javascript jquery html json

在我的html表单中,我有9个下拉值,如果用户操作是编辑,那么它将从数据库中获取值并以jsonencode格式返回,如下所示,

  

JSON DATA

 [{"ed_gender":"Male","ed_blood_group":"A-","ed_marital_status":"Single","ed_branch_id":"11","ed_desig_id":"1","ed_job_type":"Permanent","ed_pay_mode":"Cheque"}] 
  

HTML

 <select name="ed_gender" class="form-control">
    <option value="">Select</option>
    <option value="Male">Male</option>
    <option value="Female">Female</option>
 </select>

 <select name="ed_marital_status" class="form-control">
    <option value="">Select</option>
    <option value="Single">Single</option>
    <option value="Married">Married</option>
 </select> 

我尝试了几行代码使用php实际上有效,但我正在尝试使用javascript。

  

PHP

 <select name="ed_marital_status" class="form-control">
    <option value="">Select</option>
    <option <?php if($ed_marital_status=="Single") echo 'selected="selected"'; ?> value="Single">Single</option>
    <option <?php if($ed_marital_status=="Married") echo 'selected="selected"'; ?> value="Married">Married</option>
 </select> 

所以这里有行为我必须提取json值并使下拉值在页面加载时“选中”。

已编辑:

  

JSON DATA

 [{"ed_branch_id":"11","ed_desig_id":"1"}] 
  

HTML

 <select name="ed_job_location" class="form-control">
     <option value="">Select</option>
      <?php
       foreach($get_branches as $branches){
              $branches_id   = $branches->b_id;
              $branches_name = $branches->b_name; 
              $branches_code = $branches->b_code; 
              echo "<option value='$branches_id||$branches_name||$branches_code'>$branches_name</option>";
       }?>
  </select>

  <select name="ed_desig_id" class="form-control">
      <option value="">Select</option>
      <?php
      foreach($get_designation as $designations){
             $designations_id   = $designations->d_id;
             $designations_name = $designations->d_designation; 
             $designations_code = $designations->d_code; 
             echo "<option value='$designations_id||$designations_code'>$designations_name</option>";
       }?>
   </select>

这里从json开始,我只得到分支id和设计id,但是这里我有||的价值选择选项中的符号,所以我需要找到特定的ID并在下拉列表中显示它。

1 个答案:

答案 0 :(得分:2)

遍历json个对象数组,并通过名称选择它来填充每个元素值:

&#13;
&#13;
var json = [{"ed_gender":"Male","ed_blood_group":"A-","ed_marital_status":"Single","ed_branch_id":"11","ed_desig_id":"1","ed_job_type":"Permanent","ed_pay_mode":"Cheque"}];

$(document).ready(function(){
   $.each(json[0], function(index, element) {

       $("[name="+index + "]").val(element);
   });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="ed_gender" class="form-control">
    <option value="">Select</option>
    <option value="Male">Male</option>
    <option value="Female">Female</option>
 </select>

 <select name="ed_marital_status" class="form-control">
    <option value="">Select</option>
    <option value="Single">Single</option>
    <option value="Married">Married</option>
 </select>
&#13;
&#13;
&#13;

编辑:

只要值在返回的||数据中,您就可以显式设置值以涵盖分支值与循环外json串联的特殊情况:

$(document).ready(function(){

       $("[name=ed_job_location]").val(json[0].branches_id + "||" + json[0].branches_name + "||" + json[0].branches_code);

    });