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

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

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?