需要有关嵌套 jquery 函数的帮助

发布于 2024-09-09 16:15:04 字数 1511 浏览 2 评论 0原文

请在下面找到我的代码。我的问题是我内部的 jQuery.get() 没有被执行。有人可以帮我解决这个问题吗?

jQuery(document).ready(function() { 
   $('.error').hide();  
   $(".button").click(function() {  
    // validate and process form here
      $('.error').hide();  
      var zipCode = $("input#name").val();  
      if (zipCode == "") {  
        $("label#name_error").show();  
        $("input#name").focus();  
        return false;  
      }
    var key = 'ac9c073a8e025308101307';
    var noOfDays = 5;
    var url = 'http://www.worldweatheronline.com/feed/weather.ashx?q=' + zipCode + '&format=json&num_of_days=' + noOfDays + '&key=' + key;
    alert(url); 
    jQuery.get(url, function(test) { 
    alert(url); 
        $.each(test.data.weather, function(i, item){
        $("body").append("<p>Date: "+item.date+"</p>");
        if ( i == 3 ) return false;
  }); }, 'jsonp')();
});  
}); 

我的 html 表单看起来

<div id="contact_form">  
 <form name="contact" action="">  
   <fieldset>  
     <label for="name" id="name_label">Name</label>  
     <input type="text" name="name" id="name" size="30" value="" class="text-input" />  
     <label class="error" for="name" id="name_error">This field is required.</label>  

     <br />  
     <input type="submit" name="submit" class="button" id="submit_btn" value="Send" />  
   </fieldset>  
 </form>  
</div>  

非常感谢任何指点。

谢谢!

Please find my code below. My problem is that I the inner jQuery.get() doesn't get executed. Could some one help me out with this?

jQuery(document).ready(function() { 
   $('.error').hide();  
   $(".button").click(function() {  
    // validate and process form here
      $('.error').hide();  
      var zipCode = $("input#name").val();  
      if (zipCode == "") {  
        $("label#name_error").show();  
        $("input#name").focus();  
        return false;  
      }
    var key = 'ac9c073a8e025308101307';
    var noOfDays = 5;
    var url = 'http://www.worldweatheronline.com/feed/weather.ashx?q=' + zipCode + '&format=json&num_of_days=' + noOfDays + '&key=' + key;
    alert(url); 
    jQuery.get(url, function(test) { 
    alert(url); 
        $.each(test.data.weather, function(i, item){
        $("body").append("<p>Date: "+item.date+"</p>");
        if ( i == 3 ) return false;
  }); }, 'jsonp')();
});  
}); 

My html form looks like

<div id="contact_form">  
 <form name="contact" action="">  
   <fieldset>  
     <label for="name" id="name_label">Name</label>  
     <input type="text" name="name" id="name" size="30" value="" class="text-input" />  
     <label class="error" for="name" id="name_error">This field is required.</label>  

     <br />  
     <input type="submit" name="submit" class="button" id="submit_btn" value="Send" />  
   </fieldset>  
 </form>  
</div>  

I'd really appreciate any pointers.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

三五鸿雁 2024-09-16 16:15:04

您遇到的问题是,您没有采取任何措施来阻止表单的提交,因此它会在有机会从 worldweatheronline.com 获取数据之前提交表单。

将您的点击处理程序更改为如下所示:

    $(".button").click(function(event) { 
        event.preventDefault();

这是我完成的示例代码,它可以按照您希望的方式工作:

<html>
<head>
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'></script>
<script type='text/javascript'>
    jQuery(document).ready(function() { 
        $('.error').hide();  
        $(".button").click(function(event) { 
            event.preventDefault();
            // validate and process form here
            $('.error').hide();  
            var zipCode = $("input#name").val();  
            if (zipCode == "") {  
                $("label#name_error").show();  
                $("input#name").focus();  
                return false;  
            }
            var key = 'ac9c073a8e025308101307';
            var noOfDays = 5;
            var url = 'http://www.worldweatheronline.com/feed/weather.ashx?q=' + zipCode + '&format=json&num_of_days=' + noOfDays + '&key=' + key;

            alert('first'+url); 

            jQuery.get(url, function(test) { 
                alert(url); 
                $.each(test.data.weather, function(i, item){
                    $("body").append("<p>Date: "+item.date+"</p>");
                    if ( i == 3 ){
                        return false;
                    }
                }); 
            }, 'jsonp');
        });  
    });
</script>
</head>
<body>
<div id="contact_form">  
 <form name="contact" action="">  
   <fieldset>  
     <label for="name" id="name_label">Name</label>  
     <input type="text" name="name" id="name" size="30" value="" class="text-input" />  
     <label class="error" for="name" id="name_error">This field is required.</label>  

     <br />  
     <input type="submit" name="submit" class="button" id="submit_btn" value="Send" />  
   </fieldset>  
 </form>  
</div>  
</body>
</html>

The problem you are experiencing is that you dont do anything to stop your form from being submitted, thus it submits the form before it gets a chance to get the data from the worldweatheronline.com.

Change your click handler to be like this:

    $(".button").click(function(event) { 
        event.preventDefault();

Here is my completed sample code that works the way you want it to:

<html>
<head>
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'></script>
<script type='text/javascript'>
    jQuery(document).ready(function() { 
        $('.error').hide();  
        $(".button").click(function(event) { 
            event.preventDefault();
            // validate and process form here
            $('.error').hide();  
            var zipCode = $("input#name").val();  
            if (zipCode == "") {  
                $("label#name_error").show();  
                $("input#name").focus();  
                return false;  
            }
            var key = 'ac9c073a8e025308101307';
            var noOfDays = 5;
            var url = 'http://www.worldweatheronline.com/feed/weather.ashx?q=' + zipCode + '&format=json&num_of_days=' + noOfDays + '&key=' + key;

            alert('first'+url); 

            jQuery.get(url, function(test) { 
                alert(url); 
                $.each(test.data.weather, function(i, item){
                    $("body").append("<p>Date: "+item.date+"</p>");
                    if ( i == 3 ){
                        return false;
                    }
                }); 
            }, 'jsonp');
        });  
    });
</script>
</head>
<body>
<div id="contact_form">  
 <form name="contact" action="">  
   <fieldset>  
     <label for="name" id="name_label">Name</label>  
     <input type="text" name="name" id="name" size="30" value="" class="text-input" />  
     <label class="error" for="name" id="name_error">This field is required.</label>  

     <br />  
     <input type="submit" name="submit" class="button" id="submit_btn" value="Send" />  
   </fieldset>  
 </form>  
</div>  
</body>
</html>
丢了幸福的猪 2024-09-16 16:15:04

打开 Fiddler 或 Firebug 并观察 HTTP 调用。您将在那里看到 HTTP 错误消息。另外,请在 Chrome 或 Firefox 中打开控制台以查看潜在的 JavaScript 错误。

Turn on Fiddler or Firebug and watch the HTTP call go out. You'll see the HTTP error message there. Also, turn on your console in Chrome or Firefox to see a potential JavaScript error.

小伙你站住 2024-09-16 16:15:04

部分问题可能是 .get() 函数调用后的额外括号。你有:

jQuery.get(url, function(test) { 
    alert(url); 
    $.each(test.data.weather, function(i, item){
    $("body").append("<p>Date: "+item.date+"</p>");
    if ( i == 3 ) return false;
}); }, 'jsonp')();

应该是:

jQuery.get(url, function(test) { 
    alert(url); 
    $.each(test.data.weather, function(i, item){
        $("body").append("<p>Date: "+item.date+"</p>");
        if ( i == 3 ) return false;
    }); 
}, 'jsonp');

Part of the problem could be the extra set of parentheses after your .get() function call. You have:

jQuery.get(url, function(test) { 
    alert(url); 
    $.each(test.data.weather, function(i, item){
    $("body").append("<p>Date: "+item.date+"</p>");
    if ( i == 3 ) return false;
}); }, 'jsonp')();

It should be:

jQuery.get(url, function(test) { 
    alert(url); 
    $.each(test.data.weather, function(i, item){
        $("body").append("<p>Date: "+item.date+"</p>");
        if ( i == 3 ) return false;
    }); 
}, 'jsonp');
究竟谁懂我的在乎 2024-09-16 16:15:04

我将冒险猜测(基于 url 'http://www.worldweatheronline.com/feed/weather.ashx?q=' )您正在尝试执行的操作对外部站点的 AJAX 请求。这违反了相同域政策并且将会失败。

Im going to go out on a limb and guess that (based on the url 'http://www.worldweatheronline.com/feed/weather.ashx?q=' ) that you are attempting to perform an AJAX request to an external site. This violates Same Domain Policy and will fail silently.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文