如何根据选择的单选按钮更改下拉列表的值?

时间:2019-01-16 07:36:18

标签: javascript php ajax

如何获取单选按钮的值并在where子句中使用它?

        <input type="radio" name="radiobtn" value = "Yes" onclick="GetLockIn();" >Yes
        <input type="radio" name="radiobtn" value = "No" onclick="GetLockIn();" >No

            <select id="reasondd"  name="R" onChange="change_reason()"  width="40" >
            <option id="mySelect" value="" selected="selected" >Select</option> 
            <?php 

                $CheckLockin=$_GET["$CheckLockin"];

                $res=mysqli_query($link,"SELECT DISTINCT discon_reason FROM note_gen2 where lock_in_stat = '$CheckLockin' order by discon_reason asc"); 
                while ($row=mysqli_fetch_array($res)) {

            ?>
                <option value="<?php echo $row['discon_reason'];?>"><?php echo $row["discon_reason"]; ?></option>
            <?php } ?>

     /* FILE AJAX*/
    function change_reason()
    {
    var xmlhttp=new XMLHttpRequest();
    xmlhttp.open("GET", "ajax.php?reason="+document.getElementById("reasondd").value,false);
    xmlhttp.send(null);
    /* OFFER NO. DROPDOWN */
    document.getElementById("offers").innerHTML=xmlhttp.responseText;
    }


     /* FILE LOCKIN */
    function GetLockIn()
        {
            alert("Do something radio button was checked");
        }

ajax ..我希望我的下拉列表更改取决于所选的单选按钮

    <?php require(dirname(__FILE__)."/includes/connection.php");
    $reason=$_GET["reason"];

    if ($reason!="")
    {
                $res=mysqli_query($link,"SELECT offers FROM note_gen2 WHERE discon_reason='$reason'");

                    echo  '<table border ="0" align="top" id="table_data" ><tr><th colspan="0" ></th><th>OFFERS</th><th>YES</th></tr>';


                while ($row=mysqli_fetch_array($res))
                {
                    echo  '<tr ><td >'.'<input type="checkbox" name="chkboxoffers" value="'.$row['offers'].'" onchange="updateCheckbox(this);">'.
                          '</td><td >'.'<label id="color" >'.$row['offers'].'</label>'.'&nbsp'.
                          '</td><td >'.'<input type="checkbox" name="radioaccepted" value="'.$row['offers'].'" onchange="updateCheckbox(this);">'.  
                          '</td></tr >';
                }
                    echo '</table>'; 
    }
    ?>

我希望我的下拉菜单更改取决于所选的单选按钮

1 个答案:

答案 0 :(得分:0)

我已经使用ajax(带有数据库)完成了这项工作, 所以我给你发了代码,希望对你有帮助

<input type="radio" name="radiobtn" value = "Yes" class="rdio_btn" >Yes
<input type="radio" name="radiobtn" value = "No" class="rdio_btn" >No

<select id="reasondd"  name="R" onChange="change_reason()"  width="40" >
<option id="mySelect" value="" selected="selected" >Select</option> 

<script>
$(".rdio_btn").on('click' , function(){
     var data = $(this).attr('value');
     var url = /*your url*/;
     var type = /* get or post */;
     $.ajax({
          data:{'data':data , '_token': "{{csrf_field()}}" },
          type:type,
          url:url,
          dataType:'JSON',
          success:function(data){
             $.each(data, function(d)){
                 $("#reasondd").append('<option value="'+data[d].id+'" selected="selected" >'+data[d].name+'</option>');
                 });
             },error:function(e){
                    console.log('error is here');
             }
          )};
     });
});
</script>

您的控制器应该是这样

public function functionName(Request $request){
      if($request['data'] == 'yes'{
           return response(/*Your data you wanna send back if user selected yes button in your radio buttons*/);
      }else{
          return response(/*Your data you wanna send back if user selected no button in your radio buttons*/);
       }
}

希望您可以使用它