doudou3213 2013-03-03 06:20
浏览 14
已采纳

javascript显示不同的错误消息[关闭]

I have two page (page1.php and page2.php) that need to put the javascript coding. On the page1.php, there have two images link to page2.php.

This is the code for page1.php:

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

This is code for page2.php:

<input id="t_c" type="checkbox" name="t_c" value="1"/>
<input type="image" value="continue" src="images/btn_cont.png"/>

If user click image1, it will go to page2.php. then if user didn't click the checkbox, there will display the error message.

If user click image2, it will go to page2.php. then if user didn't click the checkbox, there will display different error message.

  • 写回答

2条回答 默认 最新

  • douying7289 2013-03-03 06:38
    关注

    Input type image is a submit button.

    If page 2 is a php page, the best solution is - as mentioned by jeff9888 to select the message to show on the server. If you must use JavaScript, pass the parm in the query string, it will show in location.search on pages 2

    Page 1

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

    Page2:

    <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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?