我有一个book now按钮,当用户点击book now时,他应该被重定向到订单确认页面。我想使用jquery在url中传递类型和id。我想要传递这种url - http://localhost/company/order-confirmation?type=course&id=35
我如何在jquery中做到这一点。
html:
<a href="" class="book_now" batch-id="">Book Now</a>Jquery:
$('.book_now',template).attr('batch-id', val['batch_id']);发布于 2017-03-30 15:25:56
您应该使用属性data* (see this link),因为属性"batch-id“不是有效的html。
在常规jQuery中,如果链接是这样提供的:
<a href="http://localhost/company/order-confirmation?type=course" class="book_now" data-batch-id="123">Book Now</a>
$(document).on('click', '[data-batch-id]', function(e) {
// Disable link behavior
e.preventDefault();
// Save information
var batchID = $(this).data().batchId,
href = $(this).attr('href');
// Check if any ID is aviable
if (batchID) {
// Save the url and perform a page load
var link = href + '&id=' + batchID;
window.open(link, '_blank');
} else {
// Error handling
alert('Can\'t find course, no ID provided')
}
});<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<a href="http://localhost/company/order-confirmation?type=course" class="book_now" data-batch-id="123">Book Now</a>
https://stackoverflow.com/questions/43108461
复制相似问题