2013-03-03 14 views
-1

我有兩個頁面(page1.php中則page2.php)需要把JavaScript編碼。 在page1.php上,有兩個圖像鏈接到page2.php。的JavaScript顯示不同的錯誤信息

這是在page1.php中代碼:

<a href="page2.php"><img src="btn1.png" id="img1"></a> 
<a href="page2.php"><img src="btn2.png" id="img2"></a> 

這是代碼,使page2.php:

如果用戶點擊此搜索,它會去使page2.php。那麼如果用戶沒有點擊複選框,則會顯示錯誤信息。

如果用戶點擊圖像2,它會去使page2.php。那麼如果用戶沒有點擊複選框,則會顯示不同的錯誤信息。

+0

@Leeyaginger你熟悉事件註冊嗎? – 2013-03-03 06:29:21

+0

我試圖使用如果其他但它不工作..im不知道是否條件是錯誤的或page2.php不獲取圖像ID。 – 2013-03-03 06:31:30

+0

@MatthewBlancarte nope..since即時通訊新的JavaScript .. – 2013-03-03 06:32:44

回答

0

輸入類型的圖像是提交按鈕。

如果2頁是一個PHP頁面,最好的辦法是 - 由jeff9888提到選擇信息顯示在服務器上。如果必須使用JavaScript,通過PARM在查詢字符串,它會在location.search顯示在頁面2

<a href="page2.php?parm=img1"><img src="btn1.png" id="img1"></a> 
<a href="page2.php?parm=img2"><img src="btn2.png" id="img2"></a> 

第2頁:

<hmtl> 
<head> 
<title>Page 2</title> 
<script> 
window.onload=function() { 
    document.getElementById("form1").onsubmit=function() { 
    if (!this.elements["t_c"].checked) { // note the "this" keyword 
     if (location.search.indexOf("img1")!=-1) { // ?id=img1 
     alert("Please check the checkbox for image 1"); 
     } 
     else { 
     alert("Please check the checkbox for image 2"); 
     } 
     return false; // cancel submit 
    } 
    return true; // allow submit 
    } 
} 
</script> 
</head> 
<body> 
<form id="form1"> 
    <input id="t_c" type="checkbox" name="t_c" value="1"/> 
    <input type="image" value="continue" src="images/btn_cont.png"/> 
</form> 
</body> 
</html> 
+0

謝謝..但如果用戶點擊image2,它需要顯示不同的錯誤信息。 – 2013-03-03 06:45:27

+0

查看更新..... – mplungjan 2013-03-03 06:50:03

+0

我試過了..但是當我點擊兩個圖像時,它顯示「請選中圖像2的複選框」。 – 2013-03-03 06:59:57

0

發送獲取變量你使page2.php:

<a href="page2.php?id=img1"><img src="btn1.png" id="img1"></a> 
<a href="page2.php?id=img2"><img src="btn2.png" id="img2"></a> 

然後在使page2.php,使用ID你來決定你想要什麼d錯誤信息isplay。

<?php 
if($_GET['id']=='img1') 
    $msg="Message 1"; 
else if($_GET['id']=='img2') 
    $msg="Message 2"; 
?> 
+0

感謝您的答案:)但我需要使用javascript .. – 2013-03-03 07:03:54

+0

好吧,我認爲你可以在圖片被點擊時設置cookie,然後從你的page2.php獲取cookie來定義顯示哪個錯誤消息。 – 2013-03-03 07:12:02

+0

@ jeff9888或'localStorage'來存儲信息。 – 2013-03-03 07:24:06