英文:
How to set specific query string value (or update if exists) without appending the value on click of a <a> tag?
问题
我正在尝试在单击<a>元素时更新或设置特定的URL查询字符串值。
我设法通过设置URL来使其工作:
<a id="my-link" href="http://example.com/"></a>
$('#my-link').on('click', function(e) {
let url = this.getAttribute('href');
let queryStringKey = 'someKey';
let queryStringValue = encodeURIComponent(someDataObject);
url += `?${queryStringKey}=${queryStringValue}`;
e.originalEvent.currentTarget.href = url;
});
但这仅在第一次单击时有效,因为它不断附加查询字符串。
我尝试过保存原始URL,使用e.originalEvent.currentTarget.href
设置URL,然后在单击后设置回原始URL,但它不起作用,它只读取原始URL而不是更新后的URL:
<a id="my-link" href="http://example.com/"></a>
$('#my-link').on('click', function(e) {
let originalUrl = this.getAttribute('href');
let queryStringKey = 'someKey';
let queryStringValue = encodeURIComponent(someDataObject);
let updatedUrl = originalUrl + `?${queryStringKey}=${queryStringValue}`;
e.originalEvent.currentTarget.href = updatedUrl;
this.href = originalUrl; // 它随后打开原始URL而不是updatedUrl
});
英文:
I am trying to update or set a given URL specific query string value on a click of an <a>
element.
I managed to make it work by setting the URL:
<a id="my-link" href="http://example.com/">
$('#my-link').on('click', function(e) {
let url = this.getAttribute('href');
let queryStringKey = 'someKey';
let queryStringValue = encodeURIComponent(someDataObject);
let url += `?${key}=${queryStringValue}`;
e.originalEvent.currentTarget.href = url;
});
But this only works on the first click because it keeps appending the query string.
What I tried was to then save the original URL, set the url using e.originalEvent.currentTarget.href
and then set it back after the click, but it doesn't work, it just reads the original url instead of the updated one:
<a id="my-link" href="http://example.com/">
$('#my-link').on('click', function(e) {
let originalUrl = this.getAttribute('href');
let queryStringKey = 'someKey';
let queryStringValue = encodeURIComponent(someDataObject);
let updatedUrl += originalUrl + `?${key}=${queryStringValue}`;
e.originalEvent.currentTarget.href = updatedUrl;
this.href = originalUrl; // it then opens this URL instead of the updatedUrl
});
答案1
得分: 1
使用URL API
注意:如果您没有有效的URL,可以使用new URL(this.href);
,因为new URL(this.getAttribute("href"))
在href="/"
上会失败。
$('#my-link').on('click', function(e) {
e.preventDefault(); // 为了查看控制台
let queryStringKey = 'someKey';
let queryStringValue = { x: 2 };
let url = new URL(this.href);
url.searchParams.set(queryStringKey, queryStringValue);
this.href = url.toString();
console.log(this.href)
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a id="my-link" href="/">点击</a>
英文:
Use the URL API
Note: use new URL(this.href);
if you do not have a valid URL since
new URL(this.getAttribute("href"))
will fail on href="/"
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$('#my-link').on('click', function(e) {
e.preventDefault(); // to see the console
let queryStringKey = 'someKey';
let queryStringValue = { x: 2 };
let url = new URL(this.href);
url.searchParams.set(queryStringKey, queryStringValue);
this.href=url.toString();
console.log(this.href)
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a id="my-link" href="/">Click</a>
<!-- end snippet -->
答案2
得分: -1
每当单击URL时,删除附加的键值以恢复原始链接:
// A $( document ).ready() block.
$( document ).ready(function() {
let someDataObject = 'someDataObject';
$('#my-link').on('click', function(e) {
e.preventDefault(); // to see the console
let url = this.getAttribute('href');
let queryStringKey = 'someKey';
let queryStringValue = encodeURIComponent(someDataObject);
url = url.split('?'+queryStringKey)[0];
url += `?${queryStringKey}=${queryStringValue}`;
console.log(url);
e.originalEvent.currentTarget.href = url;
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a id="my-link" href="http://example.com/">click me</a>
请注意,上述代码是用JavaScript和jQuery编写的,用于在单击链接时修改URL以删除附加的键值并恢复原始链接。
英文:
Whenever the url get clicked Remove the additional key value to restore the original link :
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
// A $( document ).ready() block.
$( document ).ready(function() {
let someDataObject = 'someDataObject';
$('#my-link').on('click', function(e) {
e.preventDefault(); // to see the console
let url = this.getAttribute('href');
let queryStringKey = 'someKey';
let queryStringValue = encodeURIComponent(someDataObject);
url = url.split('?'+queryStringKey)[0];
url += `?${queryStringKey}=${queryStringValue}`;
console.log(url);
e.originalEvent.currentTarget.href = url;
});
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a id="my-link" href="http://example.com/">click me</a>
<!-- end snippet -->
答案3
得分: -2
Sure, here is the translated JavaScript code:
$('#my-link').on('click', function(e) {
let url = this.getAttribute('href').split('?')[0]; // 移除旧的查询字符串
let queryStringKey = 'someKey';
let queryStringValue = encodeURIComponent('someValue');
url += `?${queryStringKey}=${queryStringValue}`;
e.originalEvent.currentTarget.href = url;
});
英文:
I would do so:
$('#my-link').on('click', function(e) {
let url = this.getAttribute('href').split('?')[0]; // remove old query string
let queryStringKey = 'someKey';
let queryStringValue = encodeURIComponent('someValue');
url += `?${queryStringKey}=${queryStringValue}`;
e.originalEvent.currentTarget.href = url;
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论