Jquery没有对任何事情做出反应

时间:2012-08-13 13:08:07

标签: 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>

2 个答案:

答案 0 :(得分:1)

括号中有 lot 错误。修复它们,你的代码就可以了。使用正确的缩进作为指导。

$(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

答案 1 :(得分: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);
                }
            });
        }

        });
    });