[英]How can i make drop down values Selected using Javascript by jsonencode data
In my html form i have 9 drop down values which if user action is Edit then it will fetch the values from database and return in the jsonencode format.as below, 在我的html表单中,我有9个下拉值,如果用户操作为Edit,则它将从数据库中获取值并以jsonencode格式返回。如下所示,
JSON DATA 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"}]
HTML 的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>
I tried few lines of code using php its actually works but i am trying using javascript. 我尝试了几行使用php的代码,它实际上有效,但是我正在尝试使用javascript。
PHP 的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>
So here Behave i have to extract the json values and make dropdown values has "Selected" on page loads. 因此,在这里,我必须提取json值,并使下拉值在页面加载时具有“已选择”状态。
EDITED : 编辑:
JSON DATA JSON数据
[{"ed_branch_id":"11","ed_desig_id":"1"}]
HTML 的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>
Here from above json, i am getting only branch id and desig id, But here i have value with || 这里从json上面,我只获得分支ID和设计ID,但是这里我具有||的价值。 symbol in the select option, so i need to find particular id and show it in the drop down. 选择选项中的符号,因此我需要找到特定的ID并在下拉菜单中显示它。
Iterate through the array of json
objects and fill each element value by selecting it through its name: 遍历json
对象数组,并通过其名称选择每个元素值来填充它:
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); }); });
<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>
EDIT: 编辑:
You can explicitly set the values to cover the special case of branch values concatenation with ||
您可以显式设置值,以使用||
覆盖分支值串联的特殊情况。 outside the loop, as long as the values are within the json
data returned : 在循环外部,只要值在返回的json
数据之内:
$(document).ready(function(){
$("[name=ed_job_location]").val(json[0].branches_id + "||" + json[0].branches_name + "||" + json[0].branches_code);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.