简体   繁体   English

如何从表单字段获取值并在Jquery中与url连接

[英]How to get value from form field and concatenate with url in Jquery

I am using jquery for the first time today and need help with this problem. 我今天第一次使用jquery,需要帮助解决这个问题。 For the all the pros out there, this issue might sound dumb but I am struggling. 对于那里的所有专业人士来说,这个问题可能听起来很愚蠢,但我正在努力。

I have a form field in a HTML page, and want to get value from the form field and concatenate with a url. 我在HTML页面中有一个表单字段,并希望从表单字段中获取值并与URL连接。

<form id="form-container" class="form-container">
        <label for="street">Street: </label><input type="text" id="street" value="">
        <label for="city">City: </label><input type="text" id="city" value="">
        <button id="submit-btn">Submit</button>
    </form>

This is tag where I am want to add the values of street and city. 这是标签,我想添加街道和城市的价值。

<img class="bgimg" src="https://maps.googleapis.com/maps/api/streetview?size=600x300&location=White House, Washington DC&key=API_KEY">
</body>

Basically the location field in this src will come from the form field. 基本上,此src的位置字段将来自表单字段。 so something like this: 所以这样的事情:

<img class="bgimg" src="https://maps.googleapis.com/maps/api/streetview?size=600x300&location=" + $('#street').val() + "," + $('#city').val()&key=API_KEY">
    </body>

But unfortunately this is not working and need some pointers to resolve this. 但不幸的是,这不起作用,需要一些指针来解决这个问题。

UPDATE : I am trying this method to achieve this but not working 更新:我正在尝试这种方法来实现这一目标但不起作用

$body.append('<img class="bgimg" src="https://maps.googleapis.com/maps/api/streetview?size=600x300&location=" + $('#street').val() + " " + $('#city').val() + "&key=ABC">'

You should modify the src of your image on form submit. 您应该在表单提交时修改图像的src。 Possible duplicate of this thread (you will find a detailed answer there): Changing the image source using jQuery 该线程的可能重复(您将在那里找到详细的答案): 使用jQuery更改图像源

So something like: 所以类似于:

function setSrc(){
$('.bgimg').prop('src','https://maps.googleapis.com/maps/api/streetview?size=600x300&location=' + $('#street').val() + ',' + $('#city').val() + '&key=API_KEY');
}

<form id="form-container" class="form-container" onSubmit="setSrc();">

如果你需要使用jQuery,那么设置img标签的'src'属性的值如下:

$('.bgimg').prop('src', '"https://maps.googleapis.com/maps/api/streetview?size=600x300&location=' + $('#street').val() + ',' + $('#city').val() + '&key=API_KEY')

You can do something like this: 你可以这样做:

var url = "https://maps.googleapis.com/maps/api/streetview?size=600x300&location={street},{city}&key=API_KEY";

$("#submit-btn").on('click', function() {
  var street = $('#street').val();
  var city = $('#city').val();
  var finalURL = url.replace('{street}', street).replace('{city}', city);
  $('.bgimg').attr('src',finalURL);
});

Note that you shouldn't have src attribute set in the HTML , else browser will fire a request to invalid source. 请注意,您不应在HTML设置src属性,否则浏览器将触发对无效源的请求。

Also make sure to validate the user inputs and that you've jQuery. 还要确保验证用户输入和jQuery。

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

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