2017-03-01 63 views
2

我有一个表单,用户可以从100个问题列表中选择一个特定问题。一旦更新<Select id="problem">字段,下面的MySQL查询将通过AJAX启动。AJAX - 选择具有多个值的字段填充

if(isset($_POST['action']) && ($_POST['action']=='problem_lookup')) { 

    if(isset($_POST['problem'])) { 

     // Start MySQLi connection 
     include 'connect_db.php'; 
     $db = new mysqli($dbhost,$dbuser,$dbpass,$dbname); 

     // display error if connection cannot be established 
     if($db->connect_errno > 0){ 
     die('Unable to connect to database [' . $db->connect_error . ']'); } 

     // run query 
     $sql = "SELECT Category, Department_Responsible, Alert, Experience FROM qci_problems_index_new WHERE Issue='".$_POST['problem']."' GROUP BY Issue"; 
     $result = $db->query($sql) or die(mysqli_error($db)); 

     // return data as array 
     $array = mysqli_fetch_array($result); 
     echo json_encode($array); 
    } 

} 

查询将返回几个值这是每一个表单填写到个人<select>领域中,唯一的例外是<select id="alert-dept" multiple>字段,它可以有多个值,这就是问题的所在。

现在,除alert-dept之外的所有字段都按预期填充了一个特定值,但如果查询返回2个或更多值,则该字段保持空白。我想我通过AJAX填充字段时必须使用foreach()等效的JavaScript,但由于我不知道如何做到这一点而丢失了。

这里的代码,我用它来填充查询结果中的表单字段。

<script type="text/javascript" language="javascript">             
$(function() { 
    $('#problem').change(function() {      // on change in field "problem" 

     $.ajax({           // launch AJAX connection 
      type: 'POST',         // via protocol POST 
      url: '../../plugins/MySQL/ajax_action.php', 
      data: { action: 'problem_lookup' , problem: $("#problem").val() }, 
      dataType: 'json',        // encode with JSON 
      success: function (data) 
      { 
       var data0 = data[0];       // assign row[0] result to variable = Problem Category 
       var data1 = data[1];       // assign row[1] result to variable = Dept Responsible 
       var data2 = data[2];       // assign row[2] result to variable = Alert 
       var data3 = data[3];       // assign row[3] result to variable = Experience 

       $('#problem_category').val(data0);    // insert result into field 
       $('#department').val(data1);     // insert result into field 
       $('#alert_dept').val(data2); 
       $('#experience').val(data3); 

       $('#problem_category').trigger('change');  // refresh select field via trigger('change') to show the result 
       $('#department').trigger('change'); 
       $('#alert_dept').trigger('change'); 
       $('#experience').trigger('change'); 
      }, 
     }); 
    }); 

}); 
+0

所以'data [2]'是一个类似于[['value1','value2','value3']'的数组? –

+0

对不起,是的,忘了提及它的逗号分隔值。例如。 'Person 1','Person 2','Person 3'' – Armitage2k

+0

我想我误解了你的问题 - alert-dept是否已经拥有所有必要的选项,并且data [2]将拥有一组值选择? –

回答

1

为了针对在您的选择权option,基于Ajax的结果...

相反的:

$('#alert_dept').val(data2); 


尝试:

// Deselect all options just in case. 
$('#alert_dept option').attr("selected",false); 

data2.split(',').forEach(function(item) { 

    // Select the options that match the ajax result. 
    $('#alert_dept').find("option[value='"+item.trim()+"']").attr("selected",true); 

}); 
+0

带'$('#alert_dept')的语法错误。find(“option [value ='”+ item.trim()+“']) .attr(“选择”,真正的);?'大概在他右括号 – Armitage2k

+0

?你能告诉我什么是你有确切的错误信息 –

+0

Chrome的调试器说'失踪)上线1512'这是'$(”? (“选择”,真);“.alert_dept')。find(”option [value ='“+ item.trim()+”'))。我摆弄它,但无论我在哪里放置''''或'')''都不起作用? – Armitage2k

相关问题