2017-09-24 69 views
0

我有一個基於另一種選擇從jQuery的變化是沒有做什麼應該做的

<script type="text/JavaScript"> 
    //get a reference to the select element 

    $select = $('#building'); 
    //request the JSON data and parse into the select element 
    $.ajax({ 
     url: '/api/get_building/', 
     dataType:'JSON', 
     success:function(data){ 
     //clear the current content of the select 
     //iterate over the data and append a select option 

     $.each(data, function(key, val){ 
      $select.append('<option value="' + val.id + '">' + val.as_char + '</option>'); 
     }) 
     }, 
    }); 

function getunit() { 

    //get a reference to the select element 
    $select = $('#unit'); 
    $id_lease = $('#id_lease'); 
    $id_leaseterm = $('#id_leaseterm'); 
    //request the JSON data and parse into the select element 

    var c_id = ($("select[name='building'] option:selected").attr('value')); 
    c_url = "/api/get_unit/"+c_id+"/"; 

    $.ajax({ 

     url: c_url, 
     dataType:'JSON', 
     success:function(data1){ 
     //clear the current content of the select 
     $select.empty(); 
     $select.append('<option value="-1"> Select unit </option>'); 
     $.each(data1, function(key, val){ 
      $select.append('<option value="' + val.id + '">' + val.as_char + '</option>'); 
     }) 
     getlease(); 
     }, 

    }); 

} 

function getlease() { 

    //get a reference to the select element 
    $select = $('#id_lease'); 
    $id_leaseterm = $('#id_leaseterm'); 
    //request the JSON data and parse into the select element 
    var s_id = ($("select[name='unit'] option:selected").attr('value')); 
    s_url = "/api/get_lease/"+s_id+"/"; 

    $.ajax({ 
     url: s_url, 
     dataType:'JSON', 
     success:function(data1){ 
     //clear the current content of the select 
     $select.empty(); 
     $select.append('<option value="-1"> Select lease</option>'); 
     //iterate over the data and append a select option 

     $.each(data1, function(key, val){ 
      $select.append('<option value="' + val.id + '">' + val.as_char + '</option>'); 
     }) 
     }, 

    }); 

} 



function getleaseterm() { 

    //get a reference to the select element 
    $select = $('#id_leaseterm'); 
    //request the JSON data and parse into the select element 
    var l_id = ($("select[name='lease'] option:selected").attr('value')); 
    //var l_id = 13; 
    l_url = "/api/get_leaseterm/"+l_id+"/"; 

    $.ajax({ 
     url: l_url, 
     dataType:'JSON', 
     success:function(data1){ 
     //clear the current content of the select 
     $select.empty(); 
     $select.append('<option value="-1">Select term </option>'); 
     //iterate over the data and append a select option 

     $.each(data1, function(key, val){ 
      $select.append('<option value="' + val.id + '">' + val.as_char + '</option>'); 
     }) 
     }, 

    }); 

} 
</script> 

而下面的控制更新鏈接的下拉列表中4個阿賈克斯腳本在我的形式我更新

<select name="building" id="building" onchange="getunit();"> 
      <option id="-1">Select building</option> 
     </select> 



    <select name="unit" id="unit" onchange="getlease();"> 
      <option id="-1">Select unit</option> 
    </select> 

    <select class="select" id="id_lease" name="lease"> 
    <option value="" selected="selected">---------</option> 
    </select> 

    <select class="select" id="id_leaseterm" name="leaseterm"> 
    <option value="" selected="selected"></option> 
    </select> 

我的問題是,當我更新下拉id_lease它dosent有變化的呼籲。 (它會自動從後臺生成)

因此,對於這個原因,我將使用jQuery行這一行腳本

$("#lease_id").on("change", getleaseterm()); 

,但它給了我一個錯誤404找不到值「get_leaseterm」自我假設它在頁面加載時被觸發。我所有的腳本都沒有加載。

所以問題在於最後一行。我該如何解決它?

+2

在其他潛在的問題,「變」的處理程序的表述應該是'$ (「#lease_id」)on(「change」,getleaseterm);' - 通過包含'()'您的代碼是**調用**函數並將返回值分配爲事件處理程序。 – Pointy

+0

@Pointy謝謝。所以是的,它確實解決了404錯誤的問題。但是getleaseterm()現在根本不被觸發。 –

+0

我得到了問題。這是lease_id vs id_lease。 :(。我的命名錯誤 –

回答

0

至於尖尖的規定去掉()幫助以及我打電話id錯誤

是工作的最終版本是:

$("#id_lease").on("change", getleaseterm);