2013-06-01 10 views
0

나는이 질문을 제기했습니다 Using Jquery Ajax with PHP 이것은 sloved입니다.사업부가 jquery에 의해 업데이트되지 않았습니다

다른 질문이 있습니다.

페이지가로드 될 때 국가를 선택하면 국가가 업데이트 될 때 영국을 기본 국가로 선택하지만 미국을 국가로 변경하면 드롭 다운이 업데이트되지만 드롭 다운 상자에서 상태를 선택하면 카운티가 업데이트되지 않습니다 .

Jquery Ajax 코드.

$(document).ready(function() 
     { 
       $("#txt_country").change(getCountryStates); 

      function getCountryStates() 
      { 
      var country = $("#txt_country").val(); 
      var dataString = 'country='+ country+"&a=s"; 
      $("#countrystates").html(retrieving); 
      $.ajax({ 
       type: "POST", 
       url: url+"/OPJB_cascade/location.php", 
       data: dataString, 
       cache: false, 
       success: function(html){$("#countrystates").html(html);} 
      }); 
      $("#countryregion").html('<input type="text" id="txt_region" name="txt_region" size="30" class="textField" value="" maxlength="100" />'); 
      } 

    $("#txt_states").change(getCountryCounty); 
      //$("#txt_states").bind("onchange", getCountryCounty); 

       function getCountryCounty() 
       { 
       var states = $("#txt_states").val(); 
       var country = $("#txt_country").val(); 
       alert(states); 
       var dataString = 'country='+country+'&states='+ states+"&a=r"; 
       $("#countryregion").html('Retrieving ...'); 
       $.ajax({ 
        type: "POST", 
        url: url+"/OPJB_cascade/location.php", 
        data: dataString, 
        cache: false, 
        success: function(html){$("#countryregion").html(html);} 
       }); 
       } 
}); 

PHP 코드입니다.

$action = $_REQUEST['a']; 

     if(allow_country_state == 1) 
      $states = $_REQUEST['states']; 

     if($action == 's') 
     { 
      if(allow_country_state == 1) 
      { 
       $countryStates = CountryStates::getStates($country); 
       if($countryStates) 
       { 
        $select = '<select class="selectField" name="txt_states" id="txt_states" style="width:200px;">'; 
        $select .= '<option></option>'; 
        foreach ($countryStates as $r) 
        { 
         $select .= '<option value="'.$r->code.'">'.$r->name.'</option>';  
        } 
        $select .= '</select>'; 
       } 
       else 
       { 
        $select = '<input type="text" id="txt_states" name="txt_states" style="width:200px;" class="textField" maxlength="100" />'; 
       } 

       echo $select; 
      } 
     } 
     elseif($action == 'r') 
     { 
      //include_once OPJB_LIB_PATH.DS.'class.countrycounty.php'; 
      $CountryCounty = CountryCounty::getCounties($country, $states); 
      if($CountryCounty) 
      { 
       $select = '<select class="selectField" name="txt_region" id="txt_region" style="width:200px;">'; 
       $select .= '<option></option>'; 
       foreach ($CountryCounty as $r) 
       { 
        $select .= '<option value="'.$r->code.'">'.$r->name.'</option>';  
       } 
       $select .= '</select>'; 
      } 
      else 
      { 
       $select = '<input type="text" id="txt_region" name="txt_region" style="width:200px;" class="textField" maxlength="100" />'; 
      } 

      echo $select; 
     } 

HTML 코드. 당신은 백엔드에서 데이터를 받으면

<tr> 
     <td> <label class="required_star">*</label> </td> 
     <td> <label>{lang mkey='label' skey='country'}</label> </td> 
     <td> 
     <select name="txt_country" id="txt_country" class="selectField"> 
      {html_options options=$country selected=$smarty.session.loc.country} 
     </select> 
     </td> 
     <td>&nbsp;</td> 
    </tr> 

    {if $allow_country_state == 1 } 
     <tr> 
      <td>&nbsp;</td> 
      <td> <label>{lang mkey='label' skey='countryStates'}</label> </td> 
      <td> 
      <div id="countrystates"> 
      {if $countryStates|@count > 0} 
       <select class="selectField" name="txt_states" id="txt_states"> 
        {html_options options=$countryStates selected=$smarty.session.loc.countrystates} 
       </select> 
      {else} 
       <input type="text" id="txt_states" name="txt_states" size="30" class="textField" value="{$smarty.session.loc.countrystates}" maxlength="100" /> 
      {/if} 
      </div> 
      </td> 
      <td>&nbsp;</td> 
     </tr> 
    {/if} 
    <tr> 
     <td>&nbsp;</td> 
     <td> <label>{lang mkey='label' skey='region'}</label> </td> 
     <td> 
     <div id="countryregion"> 
     {if $CountryCounty|@count > 0} 
      <select class="selectField" name="txt_region" id="txt_region"> 
       {html_options options=$CountryCounty selected=$smarty.session.loc.countryregion} 
      </select> 
     {else} 
      <input type="text" id="txt_region" name="txt_region" size="30" class="textField" value="{$smarty.session.loc.countryregion}" maxlength="100" /> 
     {/if} 
     </div> 
     </td> 
     <td>&nbsp;</td> 
    </tr> 

답변

1

, 당신은 대체 국가 DIV를 리 바인드해야합니다.

function bindCountry() { 
    $("#txt_country").change(function(){ 
     var country=$(this).val(); 
     var dataString = 'country='+ country+"&a=s"; 
     $("#countrystates").html(retrieving); 
     $.ajax({ 
      type: "POST", 
      url: url+"/OPJB_cascade/location.php", 
      data: dataString, 
      cache: false, 
      success: function(html){$("#countrystates").html(html);} 
     }); 
    }); 
} 



$(document).ready(function() { 
    bindCountry();   
}); 

$("#txt_states").change(getCountryCounty); 

function getCountryCounty() { 
    var states = $("#txt_states").val(); 
    var country = $("#txt_country").val(); 
    alert(states); 
    var dataString = 'country='+country+'&states='+ states+"&a=r"; 
    $("#countryregion").html('Retrieving ...'); 
    $.ajax({ 
     type: "POST", 
     url: url+"/OPJB_cascade/location.php", 
     data: dataString, 
     cache: false, 
     success: function(html){ 
      $("#countryregion").html(html); 
      // Notice we rebind the country 
      bindCountry(); 
     } 
    }); 
} 
+0

내 코드를 업데이트했습니다. – user1509201

+0

나는 당신이 나를 오해한다고 생각합니다. 잉글랜드와 같은 주를 선택하면 잉글랜드의 모든 카운티를 얻어야합니다. 하지만 나는 아무것도 얻지 못합니다. 하지만 페이지로드가 의미하는 바는 국가를 변경하지 않고 상태를 선택할 수 있습니다. 영국과 내 카운티는 dropdopwn 상자에 목록을 가져옵니다. 내 선택 옵션에 – user1509201

+0

코드를 추가했습니다. 이게 내가 추가 한 것입니다.