簡體   English   中英

用JavaScript返回確認窗口

[英]Return confirm window in JavaScript

我正在使用if else語句,該語句僅根據該語句顯示。 我的確認窗口不起作用。 它將直接進入注銷頁面,而無需確認。

我的代碼:

else
{
    echo "
              <td class='td1' align='center'>
                  <a href='logout.php' onclick='return confirm('Are you sure you want to Log Out?')'><img src='./buttons/logout1.png' width='50' height='35' alt='Log Out'></a>
              </td>
          </tr>
      </table>

    ";}

你要么用

 <a href="whatever" onclick="return confirm('are you sure?')"><img ...></a> 

注意返回值(確認):腳本在固有事件中返回的值決定是否運行默認瀏覽器操作;否則,返回默認值。 如果需要運行大量代碼,您當然可以調用另一個函數:

 <script type="text/javascript"> function confirm_delete() { return confirm('Are you sure?'); } </script> ... <a href="whatever" onclick="return confirm_delete()"><img ...></a> 

否則,您可以嘗試以下操作:

 onclick="javascript:return confirm('Are you sure you want to logout?')" 

只需將其替換為代碼,因為您在確認之前已放置了一個返回值,但是JavaScript使用了define關鍵字,該關鍵字將為您提供所需的功能,而不是返回確認。 只需確認即可。

 else { echo " <td class='td1' align='center'> <a href='logout.php' onclick='confirm('Are you sure you want to Log Out?')'><img src='./buttons/logout1.png' width='50' height='35' alt='Log Out'></a> </td> </tr> </table> ";} 

 <table><tr> <td class='td1' align='center'> <a href='logout.php' onclick="return confirm('Are you sure you want to Log Out?')">Log Out</a> </td> </tr> </table> 

將您的代碼更改為此

else
{
echo "
<td class='td1' align='center'>
<a href='logout.php' onclick=\"return confirm('Are you sure you want to Log Out?')\"><img src='./buttons/logout1.png' width='50' height='35' alt='Log Out'></a>
</td>
</tr>
</table>

";}
  1. 在onlclick中使用轉義序列。

小心單引號。 確認以"替換´"

這是一個示例:jsfiddle.net/37z6wpmz/

使用雙""代替'' 那沒關系 更改為onclick="confirm('Are you sure you want to Log Out?')"

<a href="logout.php" onclick="confirm('Are you sure you want to Log Out?')"><img src='./buttons/logout1.png' width='50' height='35' alt='Log Out'> </a>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM