gpt4 book ai didi

javascript - 使用javascript为html链接中的id赋值

转载 作者:行者123 更新时间:2023-11-30 10:17:45 24 4
gpt4 key购买 nike

我有以下链接,该链接将 id 传递到我的 Controller 以呈现报告。

<a href="@Url.Action("GetReport", new { type = "PDF", id = 12345})" class="btn btn-default btn-primary">Print Report</a>

我想将上面链接中的这个值 id 分配给我在下面的调用中从数据库返回的结果....

// Global variable
var DocumentID = null;

$('#Save').click(function(e) {

if (document.forms[0].checkValidity()) {

e.preventDefault();

$.ajax({
url: "/Home/SaveDetails",
dataType: "json",
type: "POST",
data: ko.toJSON(viewModel),
contentType: "application/json;charset=utf-8",
success: function(result) {
if (result > 0) {
//Assign return value to DocumentID
DocumentID = result;
alert("This work request has been successfully saved in database. The Document ID is: " + result);
} else {
alert("The Work Request was not saved, there was an issue.");
}
},
complete: function() {
// Hide loading image.
},
error: function(jqXHR, textStatus, errorThrown) {
// Handle error.
alert(errorThrown);
}
});
} else {
alert("Form is not valid");
}
});

上面的调用有效并返回一个 DocumentID 并成功显示在警报中。如果 id 具有硬编码值,则上面的链接有效;我只想让这个值动态化。

最佳答案

if (result > 0) {
//Assign return value to DocumentID
var DocumentID = result;
var href = '@Url.Action("GetReport", new { type = "PDF", id =' + DocumentID +'})';
$("a:contain('Print Report')").attr('href', href);
}

关于javascript - 使用javascript为html链接中的id赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22979048/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com