0

事情是这样的:

我有包含 3 个以上选项的下拉列表,以及是/否无线电组。我需要的是在选择选项 1 时设置单选“否”,在选择其他选项时设置单选“是”。

这是代码:

<select id="exp_days" name="exp_days">
<?php                   
    foreach($this->days as $day){
        echo '<option value="'.$day->days.'"';  
            if($day->days==$exp_days){
                echo ' SELECTED ';  
            }                           
            echo '>';

            if($day->days==1){
                echo $day->days.'&nbsp;'.'DAY';
            }else{
                echo $day->days.'&nbsp;'.'DAYS';    
            } 

            if($day->price !='0.00'){
                //echo '&nbsp;-&nbsp;'.$day->price.'&nbsp;'.$par->get('unit_price');    
                echo '&nbsp;-&nbsp;'.priceFormat($day->price,$par->get('unit_price'));
            }
        echo '</option>';
    }
?>

<input type="radio" name="p_first" value="1" checked="checked"/><label>YES</label>
<input type="radio" name="p_first" value="0"/><label>NO</label>
4

1 回答 1

1

这就是你想要的:

<script>
    function change(select){
        if(select.selectedIndex === 0){
            document.getElementById('radio1').checked=true;
            document.getElementById('radio2').checked=false;
        }else{
            document.getElementById('radio2').checked=true
            document.getElementById('radio1').checked=false;
        }

    }

    </script>



    <select id="exp_days" name="exp_days" onchange="change(this)">
<?php                   
    foreach($this->days as $day){
        echo '<option value="'.$day->days.'"';  
            if($day->days==$exp_days){
                echo ' SELECTED ';  
            }                           
            echo '>';

            if($day->days==1){
                echo $day->days.'&nbsp;'.'DAY';
            }else{
                echo $day->days.'&nbsp;'.'DAYS';    
            } 

            if($day->price !='0.00'){
                echo '&nbsp;-&nbsp;'.priceFormat($day->price,$par->get('unit_price'));
            }
        echo '</option>';
    }
?>


    <input type="radio" name="p_first" id="radio1" value="1" checked="checked"/><label>YES</label>
    <input type="radio" name="p_bg" id="radio2" value="0"/><label>NO</label>

工作小提琴显示你想要什么:http: //jsfiddle.net/vDHV7/1/

于 2013-09-18T12:14:24.927 回答