该答案提供了对代码的必要修改。
免责声明:在未
看到确切安装的情况下,请注意,可能有多种因素导致此安装无法按原样运行。我不知道您的路由是如何设置的,或者您是否正在使用Firebug或其他控制台监视ajax调用,但是这应该为您提供了构建基块:
首先,更改您的php以将数组 输出 为json编码的字符串:
public function getCars(){ $this->load->model('car_model'); $this->form_validation->set_rules('carId', 'carId', 'trim|xss_clean'); if($this->form_validation->run()){ $carId = $this->input->post('carId'); $carModels = $this->user_management_model->getCarModels($carId); // Add below to output the json for your javascript to pick up. echo json_enpre($carModels); // a die here helps ensure a clean ajax call die(); } else { echo "error"; } }然后,修改您的脚本ajax调用,以获取获取数据并将其添加到下拉列表的成功回调:
function myFunction(obj) { $('#emptyDropdown').empty() var dropDown = document.getElementById("carId"); var carId = dropDown.options[dropDown.selectedIndex].value; $.ajax({ type: "POST", url: "/project/main/getcars", data: { 'carId': carId }, success: function(data){ // Parse the returned json data var opts = $.parseJSON(data); // Use jQuery's each to iterate over the opts value $.each(opts, function(i, d) { // You will need to alter the below to get the right values from your json object. Guessing that d.id / d.modelName are columns in your carModels data $('#emptyDropdown').append('<option value="' + d.ModelID + '">' + d.ModelName + '</option>'); }); } }); }再次-这些是基本要素。使用浏览器控制台或诸如Firebug之类的工具监视AJAX请求和返回的数据,以便您可以进行适当的修改。祝好运!



