使用JQuery进行按钮重定向

时间:2013-12-26 21:13:14

标签: javascript php jquery

在我的PHP网站中,如果用户从下拉列表中选择了PayPal,我正在尝试按钮重定向到PayPal页面进行付款。通常情况下,该按钮会转到下一页以授权信用卡付款,但由于添加了PayPal,我只需要在选择PayPal时重定向该按钮,否则它仍应转到付款流程的下一页。

有人可以帮助我让我的代码工作吗?我不是很有经验,所以我很感激你的帮助。

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

我想要的是这样的:

<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 个答案:

答案 0 :(得分:1)

查看此jsfiddle
(该链接实际上不会在jsfiddle中的任何位置,因为该站点不允许跨源框架;但是,如果打开控制台,您将看到代码确实有效。)

<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 :(得分:0)

试试这个。 页面准备就绪后,它将绑定click事件。之后,只要您点击按钮,它就会检查CardType并设置位置。

$( document ).ready(function() {
    $( ".next" ).click(function() {
      if ($('#CardType').val() == 'PayPal')
          window.location.href = "...";
      else
          window.location.href = "...";
    });       
});

这是小提琴:http://jsfiddle.net/3DA5m/