简体   繁体   English

如何将变量添加到$ .ajax的URL({url:“”});

[英]How to add a variable to URL of $.ajax({ url : “ ”});

I want the user to be able to change a part of the URL address to their post code. 我希望用户能够将URL地址的一部分更改为其邮政编码。 I have a text box and button which I am hoping will submit the value to the URL. 我有一个文本框和按钮,我希望将该值提交给URL。

jQuery: jQuery的:

jQuery(document).ready(function($) {
    $.ajax({ 
        url : "http://SomeAddress.com/" + PostCode + ".json",
        dataType : "jsonp",
        success : function(parsed_json) {

HTML: HTML:

<input type="text" id="GetPostCode" />
<button id="SetPostCode">Set This Post Code</button>

jQuery: jQuery的:

$("#SetPostCode").click(function() {
    var PostCode = document.getElementById("GetPostCode").value;
    $("#GetPostCode").val(" ");
    return false;
});

I understand that the line 我明白了这一行

$.ajax({ 
    url : "http://api.wunderground.com/api/2508132ae0c7601a/geolookup/conditions/q/UK/" + PostCode + ".json",

does not work that way, but I didn't know what else to do. 不这样做,但我不知道还能做什么。 Could someone please show me what I need to do in order for this to work? 有人可以告诉我我需要做什么才能使这个工作?

jQuery(document).ready(function($) {

   var PostCode=1;
   $.ajax({ url : "http://SomeAddress.com/"+PostCode +".json",
   dataType : "jsonp"
   //.... more stuff
  });


  $("#SetPostCode").click(function() {
     PostCode = document.getElementById("GetPostCode").value;
     $("#GetPostCode").val(" ");
     return false; 
  });

});

Above would work as PostCode is now global to the jQuery and can be accessed anywhere 上面的工作方式是因为PostCode现在是jQuery的全局,可以在任何地方访问

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM