继续我的 previous question
现在我想从下面的 URL 中获取 categoryid
:
categoryid
http://localhost/badar/category/produkterbaru/CT0007?brand=BR0006,BR0010
类别 ID 是 CT0007
这是JS
$('input[type="checkbox"]').on('change', function (e) {
var data = {},
fdata = [],
loc = $('<a>', { href: window.location })[0];
$('input[type="checkbox"]').each(function (i) {
if (this.checked) {
if (!data.hasOwnProperty(this.name)) {
data[this.name] = [];
}
data[this.name].push(this.value);
}
});
// get all keys.
var keys = Object.keys(data);
var fdata = "";
// iterate over them and create the fdata
keys.forEach(function(key,i){
if (i>0) fdata += '&'; // if its not the first key add &
fdata += key+"="+data[key].join(',');
});
$.ajax({
type: "get",
url: "<?php echo $appURL; ?>/productByCategory",
data: {
"fdata": fdata
},
success: function (response) {
$('#d2d-results').html(response);
}
});
if (history.pushState) {
history.pushState(null, null, loc.pathname + '?' + fdata);
}
});
那么如何从url中获取categoryid并设置为ajax呢?
最佳答案
不需要jQuery来解析url,你可以使用简单的JS,像这样:
// Create a link tag from the URL
let link = document.createElement('a');
link.href = 'http://localhost/badar/category/produkterbaru/CT0007?brand=BR0006,BR0010';
// Take the last part of its path:
let categoryId = link.pathname.split('/').pop();
console.log(categoryId);
您的 jQuery 处理程序中似乎已经有一个这样的实例,所以只需这样做:
loc.pathname.split('/').pop()
关于javascript - jQuery 从 URL 上的过滤器搜索中获取 categoryid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49715412/