我正在使用asp.net MVC。我试图在用户点击按钮时向用户显示自定义消息。
以下是观点:
@foreach (var item in Model.ticketList)
{
var number = item.TicketId.ToString();
<tr>
<td>
@Html.ActionLink(number, "Details", new {id=item.TicketId })
</td>
<td>Title :
@Html.DisplayFor(model => item.Title)
</td>
<td> Date Submitted :
@Html.DisplayFor(model => item.DateCreated)
</td>
<td id="status"> Ticket Status :
@if (item.Status == "open")
{
<span id="open">Open</span>
@Html.ActionLink("Close","Close",new {id=item.TicketId})
}
@if (item.Status == "closed")
{
<span id="closed">Closed</span>
@Html.ActionLink("Open","Open",new {id=item.TicketId})
}
</td>
</tr>
<tr id="ticket-backcolor">
<td colspan="4">Description :
@Html.DisplayFor(model => item.Description)
</td>
</tr>
}
动作链接是一个动作控制器,用于修改字段的值。这样可行。我想要做的是显示一个确认窗口,在其中我询问用户他是否确定要关闭/开票。
这是我到目前为止所做的一切:
<script type="text/javascript">
function alert() {
confirm("are you sure you want to close this ticket ?");
}
$(function () {
$("span[id='open']").click(alert)
});
</script>
开放期权同样如此。这不起作用。有人能帮助我吗?
编辑:
<script type="text/javascript">
$(document).ready(function () {
$("td[id='status']").click(function () {
return confirm('Are you sure you want to open the ticket ? ');
});
});
</script>
<script type="text/javascript">
$(document).ready(function () {
document.getElementById("closed").click(function () {
return confirm('Are you sure you want to open the ticket ? ');
});
});
</script>
第二个脚本也不起作用。 此脚本适用于标记。无法让它适用于2个span标签。
答案 0 :(得分:3)
使用确认
var answer = confirm("Are you sure?");
if (answer) {
//Confirm true code
}else{
//confirm false code
}
答案 1 :(得分:2)
<script type="text/javascript">
function alert() {
confirm('are you sure you want to close this ticket ?');
}
$(function() {
$('#open').click( function() {
alert();
});
});
</script>
试试这个。
答案 2 :(得分:1)
请尝试使用以下代码
<script type="text/javascript">
function closeTicketConfirm() {
if (confirm("are you sure you want to close this ticket ?")) {
//Do your code for close ticket
}
}
function openTicketConfirm() {
if (confirm("are you sure you want to open this ticket ?")) {
//Do your code for open ticket
}
}
$(function () {
$("span[id='open']").click(function() {closeTicketConfirm();});
$("span[id='closed']").click(function() {openTicketConfirm();});
});
</script>
答案 3 :(得分:0)
我为我的问题找到了解决方案并且有效。
@Html.ActionLink("Close Ticket", "Close", new { id = item.TicketId }, new { onclick=" return confirm ('Are you sure you want to close ticket ?')"})
这将在采取行动之前显示确认消息。