2014-01-24 1 views
0

아래 그림과 같이 AJAX로드 국가 도시 목록이 있습니다. 대륙이 체크되면 데이터베이스에서 국가를로드합니다. 다른 대륙을 선택하면 다음여러 확인란을 선택하면 모든 데이터를 한꺼번에로드하는 방법 - Jquery AJAX

enter image description here

은 그 관련 국가로드됩니다. 그러나 저는 양국리스트를 유지하고 모든 국가를 보여주고 싶습니다.

나의 현재 JS 코드가

$("#continent input[type='checkbox']").click(function() { 
        if($(this).is(':checked')) { 
         var checked = $(this).val(); 
         var type = 'country'; 
         //alert(checked); 
         $.ajax({ 
          type: "POST", 
          url: '<?php echo JURI::root(); ?>index.php?option=com_ajaxwork', 
          data: {country : checked, type :type}, 
          beforeSend: function() { 
           $('#countries').html("<img src='/images/loading.gif' />"); 
          }, 
          success: function(e) { 
           $('#countries').html(e); 
          }, 
          error: function() { 
           alert('it broke'); 
          }, 
          complete: function() { 
          // alert('it completed'); 
          } 
         }); 
        } 
        }); 

그리고 php 코드 것은 첫 번째 열에서 여러 확인란을 선택 때 한 번에 모든 국가를로드하려면 어떻게

$countinent_id = $_POST['country']; 
    $query2 = "SELECT * FROM #__my_country WHERE continent_id = $countinent_id"; 
    $db2 =& JFactory::getDBO(); 
    $db2->setQuery($query2);   
    $rows2 = $db2->loadObjectList(); 

    foreach ($rows2 as $value) { 
     print(' 
      <fieldset class="loc_field"> 
     <label>'.$value->country_name .'</label> 
     <input type="checkbox" name="jform[supplier_country_covered]['.$value->country_id.']" value="'.$value->country_id.'"> 
      </fieldset> 
       '); 
    } 

입니다.

감사합니다.

답변

3

PHP 코드에서 fieldset에 클래스로 대륙 ID를 추가해볼 수 있습니다. 그런 다음 jQuery 코드를

$("#continent input[type='checkbox']").click(function() { 
if ($(this).is(':checked')) { 
    var checked = $(this).val(); 
    var type = 'country'; 
    //alert(checked); 
    $.ajax({ 
     type: "POST", 
     url: '<?php echo JURI::root(); ?>index.php?option=com_ajaxwork', 
     data: { country: checked, type: type }, 
     beforeSend: function() { 
     //you have to show this some other way 
     // $('#countries').html("<img src='/images/loading.gif' />"); 
     }, 
     success: function (e) { 
      $('#countries').append(e); 
     }, 
     error: function() { 
      alert('it broke'); 
     }, 
     complete: function() { 
      // alert('it completed'); 
     } 
    }); 
} 
else { 
    var checked = $(this).val(); 
    $('.' + checked).remove(); 
} 

}로 변경하십시오.

+0

감사합니다. 그러나 이것은 효과가 없습니다! 'else' 부분은 여기서 읽지 않기 때문입니다. 나는 그 문제를 잘못 이해했다고 생각합니다. 실제로 첫 번째 열에서 여러 항목을 선택하면 모든 해당 국가를로드해야합니다. 현재 마지막 체크 박스 데이터가 표시됩니다 – Miuranga

+1

그 밖의 부분은 대륙 국가를 제거해야하며 .html() 태그를 .append()로 변경 한 것입니다. 그게 효과가 있다고 생각합니다. –

+0

시도해 보셨습니까? –

관련 문제