用户检查单选按钮时自动检查单选按钮

时间:2014-08-21 05:25:00

标签: html forms radio-button

当我按下单选按钮时如何自动检查单选按钮?

我有2个表,当我点击table1中的单选按钮时,还应该检查table2中的单选按钮。

这是table1的单选按钮代码

<tr>
                    <td><input type="radio" name="carrier" <?php if (isset($carrier) && $carrier=="COD") echo "checked";?>  value="COD"></td>
                    <td><img src="img/Cash on Delivery.jpg" alt="COE" class="picture" height="90" width="125"/></td>
                    <td><p>This service is only available for Meto Manila and Metro Cebu.<p>
                        <div id='price'> Additional ₱180 </div></td>
                </tr>

这里是table2中的单选按钮,当我检查table1中的单选按钮时需要自动检查

        <tr>
            <td><input type="radio" name="carrier" <?php if (isset($payment) && $payment=="COD") echo "checked";?>  value="COD"></td>
            <td><img src="img/Cash on Delivery.jpg" height="90" width="125"/></td>
            <td><p>Pay with your Cash on Delivery (COD)<br>Choose this option if you have selected COD under shipping. Otherwise, choose other options for payment.<p></td>
        </tr>

以下是完整代码。               

    if(isset($_GET['command']) && $_GET['command']=='update'){


        $first_name=$_SESSION['first_name'];
        $email=$_SESSION['email'];
        $home_address=$_SESSION['home_address'];
        $mobile_phone=$_SESSION['mobile_phone'];
        $carrier=$_REQUEST['carrier'];
        $payment=$_REQUEST['payment'];

        $result=mysql_query("insert into customers values('','$first_name','$email','$home_address','$mobile_phone','$carrier','$payment')");

        $customerid=mysql_insert_id();
        date_default_timezone_set("Asia/Hong_Kong");
        $date=date('Y-m-d h:i:s');
        $result=mysql_query("insert into orders values('','$date','$customerid')");
        $orderid=mysql_insert_id();

        $max=count($_SESSION['cart']);
        for($i=0;$i<$max;$i++){
            $pid=$_SESSION['cart'][$i]['productid'];
            $q=$_SESSION['cart'][$i]['qty'];
            $price=get_prod_price($pid);
            mysql_query("insert into order_detail values ($orderid,$pid,$q,$price)");
        }

        header('refresh: 0; url=homeframe.html'); // to be redirected
        exit(); // para mawala ang puta, tumigil ang script.
    }

//  else if(isset($_REQUEST['success']) && $_REQUEST['success']=='1'){
//  header('refresh: 0; url=samplebrand.php');
//  $message = "Thank you for buying, You will now be redirected";
//  echo("<script type='text/javascript'>alert('$message');</script>");
//  }
    ?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Billing Info</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script language="javascript">

    function validate(){
        var f=document.form1;
        f.command.value='update';
        f.submit();
        alert("Order submitted");
    }

$('#super').change(function(){
    var radio1 = $('input:radio[name=carrier]:checked').val();
    if(radio1 == 'on'){
        $( "input:radio[name=carrier2]" ).prop( "checked", true ).checkboxradio( "refresh" );
    }
});
</script>

<style>
#price{
    color:red;
    font-size:20px;
    padding-right: 10px;
    font-family: "Times New Roman", Times, serif;
    float:right;
}
td{
        display:block;
}
</style>
</head>
<body>

    <form name="form1" onsubmit="return validate()">
    <input type="hidden" name="command" />
    <div align="center">
        <h1 align="center">Shipping and Payment</h1>
        <table border="0" cellpadding="2px">
            <tr><td>Order Total:</td><td>₱ <?php echo get_order_total()?></td><td>&nbsp;</td>
                </tr>
        </table>
                    <center><h1>Shipping Method</h1></center>
            <form method="post">
            <table width="900" border="0" align="center" cellpadding="2" cellspacing="0" id='center'>
                <tr>
                    <td><input type="radio" name="carrier" <?php if (isset($carrier) && $carrier=="LBC") echo "checked";?>  value="LBC"></td>
                    <td><img src="img/LBC.jpg" alt="LBC" class="picture"/></td>
                    <td><p>The Shipping takes 1-2 days for NCR and 2-3 days for any provincial.<p>
                        <div id='price'> Additional ₱250 </div></td>

                </tr>
                <tr>
                    <td><input type="radio" id="super" name="carrier" <?php if (isset($carrier) && $carrier=="COD") echo "checked";?>  value="COD"></td>
                    <td><img src="img/Cash on Delivery.jpg" alt="COE" class="picture" height="90" width="125"/></td>
                    <td><p>This service is only available for Meto Manila and Metro Cebu.<p>
                        <div id='price'> Additional ₱180 </div></td>
                </tr>
                <tr>
                    <td><input type="radio" name="carrier" <?php if (isset($carrier) && $carrier=="Personal") echo "checked";?>  value="Personal"></td>
                    <td><img src="img/buybranded2.jpg" alt="buybranded" class="picture"/></td>
                    <td><p>The Shipping takes 2-3 days after processing for NCR and 3-5 days for any provincial.<p>
                        <div id='price'> Additional ₱100 </div></td>
                </tr>
                <tr>
                    <td><input type="radio" name="carrier" <?php if (isset($carrier) && $carrier=="NextDayDelivery") echo "checked";?>  value="NextDayDelivery"></td>
                    <td><img src="img/NextdayDelivery.jpg" alt="NextDayDelivery" class="picture"/></td>
                    <td><p>The Shipping takes 1-2 days for NCR and 2-3 days for any provincial.<p>
                        <div id='price'> Additional ₱150 </div></td>
                </tr>
                <tr>
                    <td><input type="radio" name="carrier" <?php if (isset($carrier) && $carrier=="SameDayDelivery") echo "checked";?>  value="SameDayDelivery"></td>
                    <td><img src="img/Same day Delivery.jpg" alt="SameDayDelivery" class="picture"/></td>
                    <td><p>Available only for NCR. Get your sporting good/s the same day you purchase the item. Cutoff is 12noon.<p>
                        <div id='price'> Additional ₱250 </div></td>
                </tr>
                <tr>
                    <td><input type="radio" name="carrier" <?php if (isset($carrier) && $carrier=="PickUp") echo "checked";?>  value="PickUp"></td>
                    <td><img src="img/Pick-up.jpg" alt="Pick-Up" class="picture"/></td>
                    <td><p>Office hours: 10:00 am to 6:00 pm<p>
                        <div id='price'> Free!! </div></td>
                </tr>
            </table>
            <br>
            <br>
        <center><h1>Payment Method</h1></center>
        <table width="900" border="0" align="center" cellpadding="2" cellspacing="0" id='centerdown'>
                <tr>
                    <td><input type="radio" name="payment" <?php if (isset($payment) && $payment=="BPI") echo "checked";?>  value="BPI"></td>
                    <td><img src="img/BPI.jpg"></td>
                    <td><p>Pay by BPI bank deposit (we need confirmation of payment through email.)<p></td>
                </tr>
                <!--
                <tr>
                    <td><input type="radio" name="payment" <?php if (isset($payment) && $payment=="PayPal") echo "checked";?>  value="PayPal"></td>
                    <td><img src="img/paypal.gif"></td>
                    <td><p>Pay with your PayPal account, credit card (CB, Visa, Mastercard...), or private credit card.<p></td>
                </tr>
                -->
                <tr>
                    <td><input type="radio" name="payment" <?php if (isset($payment) && $payment=="PickUp") echo "checked";?>  value="PickUp"></td>
                    <td><img src="img/cashondelivery.gif"></td>
                    <td><p>Pick up. You have 5 days reservation period. You pay for the merchandise upon pick-up<p></td>
                </tr>
                <tr>
                    <td><input type="radio" name="carrier2" <?php if (isset($payment) && $payment=="COD") echo "checked";?>  value="COD"></td>
                    <td><img src="img/Cash on Delivery.jpg" height="90" width="125"/></td>
                    <td><p>Pay with your Cash on Delivery (COD)<br>Choose this option if you have selected COD under shipping. Otherwise, choose other options for payment.<p></td>
                </tr>
            </table>
            <table>
            <tr><td><!--<input type="submit" value="Place Order"/> --> <input type="button" value="Confirm Order" onclick="window.location='quotation.php?'"></td></tr>
            </table>
        </form>

    </div>
</form>


</body>
</html>

2 个答案:

答案 0 :(得分:2)

您可以使用JQuery实现此目的。假设您的表单包含form1和form2的id:

<script>
$(document).ready(function () {
    $("#form1 input[name='carrier']").click(function () {
        if (this.checked) {
           $("#form2 input[name='carrier']").prop("checked", true);
        } // end if checked
    });
}); // end doc ready
</script>

在添加上述代码之前,您还需要包含对JQuery库的引用:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

请注意,这只会在单击form1按钮时更新form2运营商单选按钮,而不是相反。

请参阅此JSFiddle作为示例。

答案 1 :(得分:0)

我略微更改了你的第二个电台名称,你可以使用如下的jquery自动设置它:

$('input:radio[name=carrier]').change(function(){
var radio1 = $('input:radio[name=carrier]:checked').val();
if(radio1 == 'on'){
    $( "input:radio[name=carrier2]" ).prop( "checked", true ).checkboxradio( "refresh" );
}
});

DEMO

相关问题