2017-06-09 18 views
0

我想在下面添加一個確認框來詢問用戶是否真的想要刪除文件。如何在用戶想要刪除某些內容時添加確認框?

 <input type="button" value="Delete" onclick="location.href='@Url.Action("DeleteFileFromPath", "TRecords", new { StrFilename = Server.MapPath(@imgPathArray[imgNum]) })'"> 

上面的代碼運行良好,但沒有確認框。

我發現在網上的一些文件,我想用JavaScript方法如下

<form> 
    <button type="submit" onclick="DeleteConfirmBox()">Delete</button> 
</form> 

<script> 
    function DeleteConfirmBox() { 
     if (confirm("Do u want to delete the file?")) { 
      @Url.Action("DeleteFileFromPath", "TRecords", new { StrFilename = Server.MapPath(@imgPathArray[imgNum]) }); 
     } 
     else { 
      return false; 
     } 
    } 
</script> 

上面的代碼似乎沒有觸發DeleteConfirmBox()函數,我怎麼能做到這一點有確認框?

+0

你應該看看sweetalert,它的一個很好的確認警告。那就是如果你有所需的最小jQuery文件。 https://limonte.github.io/sweetalert2/ –

回答

1

您需要重定向到您的網址。替換此:

@Url.Action("DeleteFileFromPath", "TRecords", new { StrFilename = Server.MapPath(@imgPathArray[imgNum]) }); 

與此:

location.href='@Url.Action("DeleteFileFromPath", "TRecords", new { StrFilename = Server.MapPath(@imgPathArray[imgNum]) })' 
+1

是的,因爲@ Url.Action打印文字的網址。而JavaScript不會知道它需要去那裏。 – Tvde1

+0

大衛,我改變了,似乎重定向轉到索引頁面,如果我將代碼更改爲location.href ='www.abc.com',它甚至不會去鏈接www.abc.com。 ..但它真的去括號中的代碼「if(確認(」你想刪除文件嗎?「))」{ – kaihong

+0

這就是我寫的。你應該改變括號中的代碼重定向到url。 '@ Url.Action'只爲你生成url,你需要自己重定向到它。 –

0

你好凱宏修改代碼,寫按鈕類型=「按鈕」,而不是按鈕類型=「提交」,如下代碼寫的,希望它可以幫助。

<form> 
<button type="button" onclick="DeleteConfirmBox()">Delete</button> 
</form> 
<script> 
function DeleteConfirmBox() { 
    if (confirm("Do u want to delete the file?")) { 
    location.href='@Url.Action("DeleteFileFromPath", "TRecords", new { StrFilename = Server.MapPath(@imgPathArray[imgNum]) })'; 
    } 
    else { 
     return false; 
    } 
} 

+0

它的工作原理!非常非常感謝你莫漢!!!!!我很激動! – kaihong

+0

你好@凱鴻不要忘了標記它是正確的答案,如果它工作正常。 –

+0

對不起,莫漢,可能是因爲我的名聲低於15,我不能投票,並且標記正確答案 – kaihong

相關問題