[英]How to incorporate filling in saved values on autocomplete selects
我有2個選擇的自動填充功能。 您選擇的第一個,然后根據該選擇填充第二個。 我的意思是,我可以將記錄保存到提交數據中,但是如何獲取該記錄的保存值並將這些值選擇為選定的值。 這是我的自動完成部分代碼。
這是2個字段的html。
<div>
<label for="incimechtype">Incident Mechanism Type</label>
<select class="ehselect" name="incimechtype" id="incimechtype">
<option></option>
<option value="Mechanism">Mechanism</option>
<option value="Object">Object</option>
<option value="Other">Other</option>
</select>
<label for="injmechid">Incident Mechanism</label>
<select id="injmechid" name="injmechid">
</select>
</div>
這是ajax jquery腳本
$(function(){
$("#incimechtype").change(function(){
var dropdown = $(this).val();
$.ajax({
url:"getinjuryjson.php",
dataType: "json"
}).done( function(data){
$("#injmechid").find("option").remove();
if(dropdown !== ""){
$("#injmechid").append($('<option/>'));
}
switch(dropdown){
case "Mechanism":
$.each(data, function(key,value){
if(value.injmech==='Mechanism'){
$("#injmechid").append($('<option/>',{
value: value.injmechid,
text: value.injmechdescrip
}));
}
});
break;
case "Other":
$.each(data, function(key,value){
if(value.injmech==='Other'){
$("#injmechid").append($('<option/>',{
value: value.injmechid,
text: value.injmechdescrip
}));
}
});
break;
case "Object":
$.each(data, function(key,value){
if(value.injmech==='Object'){
$("#injmechid").append($('<option/>',{
value: value.injmechid,
text: value.injmechdescrip
}));
}
});
break;
}
}
)
}
)
}
);
這是php代碼。
<?php
include('common.php');
if(!($_SESSION['incident']['mechenisms'])){
$query = "select injmechid,RTRIM(injmechdescrip)injmechdescrip,RTRIM(injmech)injmech from hrs.injurymech";
$result = sqlsrv_query($link,$query,array(),array( "Scrollable" => 'static' )) or die('Cannot get injury mechanism list');
while ($row = sqlsrv_fetch_array($result,SQLSRV_FETCH_ASSOC)){
$_SESSION['incident']['mechenisms'][] =$row;
}
}
echo json_encode($_SESSION['incident']['mechenisms']);
現在,當我去編輯已保存的表單時,如何整合存儲的記錄數據以在表單中選擇正確的選項。
我想我將變量連接到ajax調用,然后在數據庫中查詢2個字段值,然后以某種方式選擇這些值(如果存在)。
我想到了。 我沒有通過ajax做到這一點。 我在頁面加載時使用php做到了。
我最終使用php來填充答案。 如果答案要改變,我將動態ajax保留在原位。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.