1

I am working with asp.net MVC. I am trying to show a custom message to a user when he clicks a button.

Here is the view :

@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>

}

The action link is a action controller to modify the value of a field. That works. What I want to do is to show a confirm window in which i ask the user if he is sure he wants to close/open a ticket.

This is what i have been trying to do so far :

<script type="text/javascript">
function alert() {
    confirm("are you sure you want to close this ticket ?");
}
$(function () {
    $("span[id='open']").click(alert)
});
</script>

And the same for the open option. This does not work. Can someone help me ?

Edit :

<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>

The second script does not work either. this script works for the tag. can not get it to work for the 2 span tags.

4

4 回答 4

3

使用确认

var answer = confirm("Are you sure?");
if (answer) {
  //Confirm true code
}else{
  //confirm false code
}
于 2013-04-18T10:31:58.133 回答
2
<script type="text/javascript">
function alert() {
    confirm('are you sure you want to close this ticket ?');
}
$(function() {
    $('#open').click( function() {
        alert();
    });
});
</script>

试试这个。

于 2013-04-18T10:36:32.067 回答
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>
于 2013-04-18T10:37:42.237 回答
0

我找到了我的问题的解决方案并且它有效。

@Html.ActionLink("Close Ticket", "Close", new { id = item.TicketId }, new  { onclick=" return confirm ('Are you sure you want to close ticket ?')"})

这将在执行操作之前显示确认消息。

于 2013-04-18T11:08:43.967 回答