依赖下拉列表和输入未填充(Codeigniter,Ajax,jQuery)

时间:2016-06-28 09:47:58

标签: php jquery ajax codeigniter onchange

我将尝试使用级联依赖选择框并在Codeigniter中使用Ajax输入。第一步非常有效。选择账户时,可以轻松加载证券。问题从第二步开始。因此,当我尝试安全选择以设置适当的可更改输入时,安全选择将被阻止,然后什么都不起作用。不明白问题是什么。 请帮助我,解决令人讨厌的堵塞。感谢。

的Ajax

$(document).ready(function(){
  var _form = $("#trans_form").serializeArray();
  $('#amount_section').hide();
  $('#quantity_section').hide();
  $('#accountDrop').on('change',function(){
    $("#securityDrop > option").remove();
    var accountID = $(this).val();
    if(accountID == '#') {return false;}
    $.ajax({
      data: _form, 
      type: "POST", 
      url: global_base_url + "custody/get_securities_dropdown/" + accountID, 
      success: function(securities) {
          $.each(securities,function(id,value) {
              var opt = $('<option />');
              opt.val(id);
              opt.text(value);
              $('#securityDrop').append(opt);
          });
      }
    });
});

$('#securityDrop').on('change',function(){
  $('#amount_section').hide();
  $('#quantity_section').hide();
    var securityID = $(this).val();
    if(securityID == '#') {return false;}
    $.ajax({
      data: _form, 
      type: "POST", 
      url: global_base_url + "custody/get_security_unit_ajax/" + securityID, 
      success: function(securUnit) {
        if (securUnit == "UNIT") {
          $('#quantity_section').show(300);
        };
        else if (securUnit == "FAMT") {
          $('#amount_section').show(300);
        };
      }
    });
});

});

控制器

public function get_securities_dropdown($account_id){
        $securities = $this->custody_model->get_security_by_account($account_id);
        header('Content-Type: application/x-json; charset=utf-8');
        echo json_encode($securities);
    }

public function get_security_unit_ajax($security_id){
    $securUnit = $this->custody_model->get_security_unit($security_id);
    header('Content-Type: application/x-json; charset=utf-8');
    echo json_encode($securUnit);
}

模型

public function get_accounts_dropdown(){
        $accounts = $this->db->select("ID as id, Account_Desc as descr")
                            ->order_by("descr", "ASC")
                            ->get($this->table2)->result();
        $accounts_arr;
        $accounts_arr['#'] = '-- Please select Account --';
        foreach ($accounts as $account) {
            $accounts_arr[$account->id] = $account->descr;
        }
        return $accounts_arr;
    }

    public function get_security_by_account($account_id){
        if(!is_null($account_id)){
            $securities = $this->db->where("a.ID_Account", $account_id)
                        ->select("b.ID as id, b.Security_Desc as descr")
                        ->join($this->table5 . " as b", "b.ID = a.ID_Security")
                        ->order_by("descr", "ASC")
                        ->get($this->table6 . " as a");
            if($securities->num_rows() > 0){
                $securities_arr;
                foreach ($securities->result() as $security) {
                    $securities_arr[$security->id] = $security->descr;
                }
                return $securities_arr;
            }
        }
        return;
    }

查看

<?php echo form_open_multipart(site_url("custody/add_transaction_pro"), array("id" => "trans_form")) ?>
    <div>
        <label for="accountDrop">Account</label>
        <div> 
          <?php echo form_dropdown('accountDrop', $account_arr, '#', 'id="accountDrop"'); ?>
        </div>
    </div>
    <div id="security_section">
        <label for="security_select">Security</label>
        <div>
<select name="securityDrop" class="required" id="securityDrop">
              <option value="#">-- Please select Security --</option>
            </select>
        </div>
    </div>
<div id="quantity_section">
        <label for="quantity">Quantity</label>
        <div id="quantityInput">
            <input type="text" id="quantity" name="quantity">
        </div>
    </div>
    <div id="amount_section">
        <label for="settl_amount">Amount</label>
        <div id="amountInput">
            <input type="text" id="settl_amount" name="settl_amount">
        </div>
    </div>

1 个答案:

答案 0 :(得分:0)

我已经解决了。错误非常简单,ajax部分中不必要的分号。语法为if() { } else if{ }