如何使用 jQuery 初始化 href
How to initialize href using jQuery
我正在尝试使用以下代码附加我的 href 属性值
$(document).ready(function() {
jQuery("#help_url").click(function(){
var href = $(this).attr('href');
alert(href);
var txt_api_key = $('#txt_api_key').val();
var txt_postcode = $('#txt_postcode').val();
var txt_countrycode = $('#txt_countrycode').val();
$('a#help_url').attr('href', href+txt_postcode+"/"+txt_countrycode+"?api_key="+txt_api_key);
});
});
这很完美。但问题是第一次点击后 link 会变成 http://api.fastway.org/v1/psc/pickuprf/2148/1?api_key=5340c900251a6105a19a58a1590472bb and on the next click after I changing the "txt_countrycode" value or if not chnaging any value it becomes http://api.fastway.org/v1/psc/pickuprf/2148/1?api_key=5340c900251a6105a19a58a1590472bb2148/1?api_key=5340c900251a6105a19a58a1590472bb
即再次添加 "href+txt_postcode+"/"+txt_countrycode+"?api_key="+txt_api_key"
我该如何解决这个问题?
一个简单的解决方案是读取 dom 中 href 的值并在点击处理程序中使用它,这样 href
将具有未更新的值
$(document).ready(function () {
var href = jQuery('#help_url').attr('href');
jQuery("#help_url").click(function () {
alert(href);
var txt_api_key = $('#txt_api_key').val();
var txt_postcode = $('#txt_postcode').val();
var txt_countrycode = $('#txt_countrycode').val();
$('a#help_url').attr('href', href + txt_postcode + "/" + txt_countrycode + "?api_key=" + txt_api_key);
});
});
当您单击按钮时,单击事件将调用。
单击事件函数与现有 url.So 串联现有 url 附加国家代码
我正在尝试使用以下代码附加我的 href 属性值
$(document).ready(function() {
jQuery("#help_url").click(function(){
var href = $(this).attr('href');
alert(href);
var txt_api_key = $('#txt_api_key').val();
var txt_postcode = $('#txt_postcode').val();
var txt_countrycode = $('#txt_countrycode').val();
$('a#help_url').attr('href', href+txt_postcode+"/"+txt_countrycode+"?api_key="+txt_api_key);
});
});
这很完美。但问题是第一次点击后 link 会变成 http://api.fastway.org/v1/psc/pickuprf/2148/1?api_key=5340c900251a6105a19a58a1590472bb and on the next click after I changing the "txt_countrycode" value or if not chnaging any value it becomes http://api.fastway.org/v1/psc/pickuprf/2148/1?api_key=5340c900251a6105a19a58a1590472bb2148/1?api_key=5340c900251a6105a19a58a1590472bb
即再次添加 "href+txt_postcode+"/"+txt_countrycode+"?api_key="+txt_api_key"
我该如何解决这个问题?
一个简单的解决方案是读取 dom 中 href 的值并在点击处理程序中使用它,这样 href
将具有未更新的值
$(document).ready(function () {
var href = jQuery('#help_url').attr('href');
jQuery("#help_url").click(function () {
alert(href);
var txt_api_key = $('#txt_api_key').val();
var txt_postcode = $('#txt_postcode').val();
var txt_countrycode = $('#txt_countrycode').val();
$('a#help_url').attr('href', href + txt_postcode + "/" + txt_countrycode + "?api_key=" + txt_api_key);
});
});
当您单击按钮时,单击事件将调用。 单击事件函数与现有 url.So 串联现有 url 附加国家代码