2012-08-13 98 views
0

我有這個jquery片段。既然它沒有把價值傳遞給我,我會用各種方法來弄清楚爲什麼,包括函數回調以查看發生了什麼。然而沒有顯示。兩個不同的例子jquery沒有任何反應

<script src="http://code.jquery.com/jquery-latest.js"></script> 
<script type="text/javascript"> 
//jquery code for source list 
$(document).ready(function() 
    { 
     $('#country').change(function() 
     { 
      if ($(this).val()!='') 
       { 
       $("#source").load("/CI-3/application/controllers/control_form.php", {pais_id: $(this).val()}, function() 
      { 
      alert('Load was not performed.')); 
      } 

    }); 

}); // end of country and city function 
</script> 

因此,這是一個應該在改變值激活jQuery的選擇列表的:

<?php echo form_open('control_form/add_all'); ?> 
     <label for="country">Country<span class="red"></span></label> 
     <select id="country" name="country"> 
      <option value="">Select</option> 
      <?php 

       foreach($result as $row) 
       { 
       echo '<option value="' . $row->pais_id . '">' . $row->pais_name . '</option>'; 
       } 

      ?> 
     </select> 

和不同的例子:

<!DOCTYPE html> 
<html> 
<head> 
<script src="http://code.jquery.com/jquery-latest.js"></script> 
<script type="text/javascript"> 
$(document).ready(function() 
     { 
      $('#country').change(function() 
        { 
        if ($(this).val()!='') 
         { 
           $("#loquesea").load("/not-here.php", function(response, status, xhr) 
            { 
           if (status == "error") 
             { 
              var msg = "Sorry but there was an error: "; 
              $("#error").html(msg + xhr.status + " " + xhr.statusText); 
              } 
           }); 


         }); 
        }); 

       }); 



</script> 
</head> 
<body> 
<b>Successful Response (should be blank):</b> 
<div id="success"></div> 
<b>Error Response:</b> 
<div id="error"></div> 


<form action=""> 
<select id = "country" name="country"> 
<option value="volvo">Volvo</option> 
<option value="saab">Saab</option> 
<option value="fiat">Fiat</option> 
<option value="audi">Audi</option> 
</select> 
</form> 
</body> 
+1

查看Chrome開發工具上的Network選項卡或Firebug中的Net選項卡,查看請求是否實際發送..以及返回結果。 – 2012-08-13 13:09:32

+0

沒有反應,並顯示當我使用選擇列表,如預期。 – iaintunderstand 2012-08-13 13:24:49

回答

1

沒有元素在你的HTML ID爲源,以便您選擇將$("#source")

變化

$("#source") 

$("#success") 

}架失蹤了if ($(this).val() != ''){

$(document).ready(function() { 
    $('#country').change(function() { 
     if ($(this).val() != '') { 
      $("#loquesea").load("/not-here.php", function(response, status, xhr) { 
       if (status == "error") { 
        var msg = "Sorry but there was an error: "; 
        $("#error").html(msg + xhr.status + " " + xhr.statusText); 
       } 
      }); 
     } 

     }); 
    }); 
+0

這不是OP要求的。此外,我們不知道HTML的其餘部分是否存在這樣的元素(OP沒有發佈的其餘元素);無論如何,問題在於該值沒有返回,很可能是因爲語法錯誤。 – Cranio 2012-08-13 13:18:44

+0

是的,這也是我添加了一個問題 – Adil 2012-08-13 13:19:31

+0

的問題之一是的,我已經刪除了我的downvote,沒有看到第二個HTML,對不起夥伴。 – Cranio 2012-08-13 13:19:53

1

很多錯誤與括號。修復它們,你的代碼將工作。使用正確的縮進作爲指導。

$(document).ready(
    function() 
    { 
     $('#country').change(
      function() 
      { 
       if ($(this).val()!='') 
       { 
         $("#source").load("/CI-3/application/controllers/control_form.php", 
              {pais_id: $(this).val()}, 
              function() 
                { 
              alert('Load was not performed.'); // 1st fix 
               } 
             ); // 2nd fix 

        } 
       } // 3rd fix... and so on