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

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

报告相同问题?

悬赏问题

  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1