doujiu8918 2013-12-26 21:13
浏览 61
已采纳

使用JQuery进行按钮重定向

In my PHP site, I am trying to get a button to redirect to a PayPal page for payment if a user has chosen PayPal from the dropdown. Normally, the button would go to the next page to authorize the credit card payment, but since the addition of PayPal, I need that button to redirect only if PayPal is chosen, otherwise it should still go to the next page in the payment process.

Can someone help me make my code work? I'm not very experienced, so I appreciate any help.

<tr>
    <td class="required req_poss" colspan="2">
        <label>Card Type <span>*</span>
        <select name="CardType" id="CardType">
             <option value="" selected="selected">--Please Select--</option>
             <option value="Amex">American Express</option>
     <!--<option value="Discover">Discover</option>-->
             <option value="MasterCard">MasterCard</option>
     <option value="PayPal">PayPal</option>
             <option value="Visa">Visa</option>
         </select>
         </label>
     </td>
 </tr>

 <tr>
     <td colspan="2" align="right"><br />
         <button type="button" class="prev" style="float:left">&laquo; Back</button>
         <button type="button" class="next right">Proceed &raquo;</button>
     </td>
 </tr>

What I want is something like this:

<script type="text/javascript">
    if ($('#CardType').val() == 'PayPal'){
        $('next').click(window.location.href = "https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=BUSINESSNAME");    
    }
</script>
  • 写回答

2条回答 默认 最新

  • duanbogan5878 2013-12-26 21:16
    关注

    check out this jsfiddle
    (the link won't actually go anywhere in jsfiddle because the site doesn't allow cross-origin framing; however, if you open up your console, you'll see that the code does in fact work.)

    <select id="CardType">
            <option value="PayPal">PayPal</option>
    </select>
    
    <input type="text" id="orderAmt" />
    
    <button class="next">Next</button>
    
    /*this would go in an external script file and placed in the header
    of your html document after your jQuery script tag; or this could
    go in a script tag at the end of the body of your html doc*/
    
     $(function() {
        $('.next').on('click',function() {
            var val = $('#CardType').val(),
                orderAmt = $('#orderAmt').val();
    
            /*the typeof argument adds security by checking to see
              that the input is a number, and not something else like a string*/
    
            if (val === 'PayPal' && typeof orderAmt === "number") {
    
                /*obviously here the url would need be changed as you see fit,
                  but this is just an example to show how to add variables as
                  PHP parameters to the end of your url*/
    
                window.location.href = "https://www.paypal.com/?amount=" + orderAmt;
            }
        });
     });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了