douzhouhan4618 2013-05-07 04:10
浏览 93
已采纳

javascript重定向到使用onclick函数返回页面

I am trying to direct back to a page "jerseyCollection" if there is no value on price of total shop. Here is the code. I am including all the code because I think there is something crashing with onclick="checkOrder()" function which contains code to redirect user back to order page. Here is the code for function. I know checkOrder() works because I have tried it with alert and i know redirecting statement works because I have tried it in other pages, too. But I just can't figure it out why it is not directing back to the shopping page even with no value in total shopping value.. Please help me figure this out. and how to solve this.

function checkOrder(){
var x = document.choice.realTotal.value 
    if (x == false){
        alert ("Please do the shopping");
        window.location.href='jerseyCollection.php';
    }
}

<

    !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Jersey Collection</title>
        <link href="general.css" type="text/css" rel="stylesheet" />
        <style type="text/css">
            table {
                    border: 4px solid #333333; 
                    width: 80%;    
                    margin-left: auto;
                    margin-right: auto; }
            td {
                    padding: 8px;
                    border 1px solid #333333;
                    background-color: #ccc; 
                    text-align: center; }
        </style>
        <script type="text/javascript" src="general.js" />
        </script>
    </head>
<body>
       <div>
        <div id="sidebar">
                <div style="color:#CCC">
                    <h2><a href="index.html">James' NBA Jersey Shop</a></h2>
                </div>
                <div>
                <FORM>
                    <p><INPUT Type="BUTTON" VALUE="Personal Info" ONCLICK="window.location.href='personalInfo.php'" /></p> 
                    <p><INPUT Type="BUTTON" VALUE="Jersey Collection" ONCLICK="window.location.href='jerseyCollection.php'" disabled /></p>
                    <p><INPUT Type="BUTTON" VALUE="Confirmation" ONCLICK="window.location.href='confirmation.php'" disabled /></p>
                    <p><INPUT Type="BUTTON" VALUE="Order" ONCLICK="window.location.href='order.html'" /></p>
                    <br>
                    <p><INPUT Type="reset" VALUE="Reset Order Info" /></p>
                    <br>
                    <br>
                    <p>Web Style Option</p>
                    <p><input name ="stylish" value="DGH" onclick="styleChange('DGH')" type="radio" checked="true" />Dark Gray & Gray & Helvetica</p> 
                    <p><input name ="stylish" value="RON" onclick="styleChange('RON')" type="radio" />Red & Orange & New York</p>  
                    <p><input name ="stylish" value="SBC" onclick="styleChange('SBC')" type="radio" />Sky Blue & Blue & Comic Sans</p> 

                </FORM>
                </div>
            </div>
    <div id="contents">
        <div>
            <h1 class="title">Jersey Collection</h1>      
        </div>
        <form name="choice" action="http://i6.cims.nyu.edu/~jwj243/Assignment5/confirmation.php" method="post">
        <table align="center">
            <tr>
                <td><img src="atlanta_josh_smith.jpg" width="200" height="200"/></td>
                <td><img src="boston_paul_pierce.jpg" width="200" height="200"/></td>
                <td><img src="brooklyn_deron_williams.jpg" width="200" height="200"/></td>
                <td><img src="chicago_derrick_rose.jpg" width="200" height="200"/></td>
            </tr>
            <tr>
                <td>Price: $20<br>Quantity<input name="PROD_ajs_20" id="q" size=2 type="text" onChange = "calculateTotal(this.form)" value= 
                <?php if(isset($_COOKIE['name'])) echo $PROD_ajs_20; else echo 0;  ?> ></td>
                <td>Price: $25<br>Quantity<input name="PROD_bpp_25" id="q" size=2 type="text" onChange = "calculateTotal(this.form)" value=
                <?php if(isset($_COOKIE['name'])) print $PROD_bpp_25; else echo 0;  ?> ></td>
                <td>Price: $25<br>Quantity<input name="PROD_bdw_25" id="q" size=2 type="text" onChange = "calculateTotal(this.form)" value=
                <?php if(isset($_COOKIE['name'])) print $PROD_bdw_25; else echo 0; ?> ></td>
                <td>Price: $30<br>Quantity<input name="PROD_cdr_30" id="q" size=2 type="text" onChange = "calculateTotal(this.form)" value=
                <?php if(isset($_COOKIE['name'])) print $PROD_cdr_30; else echo 0; ?> ></td>
            </tr>
            <tr>
                <td><img src="houston_jeremy_lin.jpg" width="200" height="200"/></td>
                <td><img src="la_blake_griffin.jpg" width="200" height="200"/></td>
                <td><img src="la_kobe_bryant.jpg" width="200" height="200"/></td>
                <td><img src="miami_dwyane_wade.jpg" width="200" height="200"/></td>
            </tr>
            <tr>
                <td>Price: $25<br>Quantity<input name="PROD_hjl_25" id="q" size=2 type="text" onChange = "calculateTotal(this.form)" value=
                <?php if(isset($_COOKIE['name'])) echo $PROD_hjl_25; else echo 0; ?>  ></td>
                <td>Price: $30<br>Quantity<input name="PROD_lbg_30" id="q" size=2 type="text" onChange = "calculateTotal(this.form)" value=
                <?php if(isset($_COOKIE['name'])) echo $PROD_lbg_30; else echo 0; ?>  ></td>
                <td>Price: $30<br>Quantity<input name="PROD_lkb_30" id="q" size=2 type="text" onChange = "calculateTotal(this.form)" value=
                <?php if(isset($_COOKIE['name'])) echo $PROD_lkb_30; else echo 0; ?>  ></td>
                <td>Price: $30<br>Quantity<input name="PROD_mdw_30" id="q" size=2 type="text" onChange = "calculateTotal(this.form)" value=
                <?php if(isset($_COOKIE['name'])) echo $PROD_mdw_30; else echo 0; ?>  ></td>
            </tr>
            <tr>
                <td><img src="miami_lebron_james.jpg" width="200" height="200"/></td>
                <td><img src="ny_carmelo_anthony.jpg" width="200" height="200"/></td>
                <td><img src="okc_kevin_durant.jpg" width="200" height="200"/></td>
                <td><img src="orlando_dwight_howard.jpg" width="200" height="200"/></td>
            </tr>
            <tr>
                <td>Price: $35<br>Quantity<input name="PROD_mlj_35" id="q" size=2 type="text" onChange = "calculateTotal(this.form)" value=
                <?php if(isset($_COOKIE['name'])) echo $PROD_mlj_35; else echo 0; ?>  ></td>
                <td>Price: $35<br>Quantity<input name="PROD_nca_35" id="q" size=2 type="text" onChange = "calculateTotal(this.form)" value=
                <?php if(isset($_COOKIE['name'])) echo $PROD_nca_35; else echo 0; ?>  ></td>
                <td>Price: $35<br>Quantity<input name="PROD_okd_35" id="q" size=2 type="text" onChange = "calculateTotal(this.form)" value=
                <?php if(isset($_COOKIE['name'])) echo $PROD_okd_35; else echo 0; ?>  ></td>
                <td>Price: $30<br>Quantity<input name="PROD_odh_30" id="q" size=2 type="text" onChange = "calculateTotal(this.form)" value=
                <?php if(isset($_COOKIE['name'])) echo $PROD_odh_30; else echo 0; ?>  ></td>

            </tr>            

        </table>
        <div>
            <table>
                <tr>
                    <td>Total: <input type="text" id ="realTotal" name="realTotal" size=2 value = <?php if(isset($_COOKIE['name'])) echo $realTotal; ?> ></td>
                    <td><span name="order_total" id="order_total" style="text-align: right; font-size: 20px">$0.00</span></td>
                </tr>
                <tr>
                    <td><input type= "submit" value="Next" onclick="checkOrder()" ><input type="hidden" name="submitted" value ="true" /></td>
                    <td></td>
                </tr>
            </table>
        </div>
        </form>
     <br/><br/><br/><br/><br/><br/><br/>   
    </div>
</div>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • douhu2898 2013-05-07 04:36
    关注

    Try this:

    function checkOrder(){
        var x = document.choice.realTotal.value;
        if (x == false){
            alert ("Please do the shopping");
            window.location.href='jerseyCollection.php';
            return false;
        }
    }
    

    In your code, the submit button looks like this:

    <td><input type= "submit" value="Next" onclick="checkOrder()" ><input type="hidden" name="submitted" value ="true" /></td>

    Please change it to:

    <td><input type= "submit" value="Next" onclick="return checkOrder()" ><input type="hidden" name="submitted" value ="true" /></td>.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常