而不使用PHP中的提交按钮

而不使用PHP中的提交按钮

本文介绍了回声单选按钮值,而不使用PHP中的提交按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在不使用提交按钮的情况下回显选定的单选按钮值.我想在选择单选按钮时显示其值.这是代码.

I want to echo the selected radio button value without using submit button. I want to display the value of radio button when it is selected. Here is the code.

   <?php
if(!empty($_GET['a1'])){
      $selected = $_GET['a1'];
}
else{
      //if no option was selected, set home as default
      $selected = 'home';
}
?>
<form action="" method="post">
         <input type="radio" name="a1" value="home"  /> Home <?php echo ($selected == 'home' ? 'This was selected!' : '');?> </br>
         <input type="radio" name="a1" value="site1" /> Site 1 <?php echo ($selected == 'site1' ? 'This was selected!' : '');?> </br>
         <input type="radio" name="a1" value="site2" /> Site 2 <?php echo ($selected == 'site2' ? 'This was selected!' : '');?> </br>
</form>

我想要以下格式的输出

I want the output in the following format

推荐答案

这是您的解决方案....

Here is your solution....

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>//jQuery Plugin
<?php
if(!empty($_GET['a1'])){ $selected = $_GET['a1'];}
else{ $selected = 'home';}
?>
<form action="" method="post">
    <label>
        <input type="radio" name="a1" value="home" /> Home
    </label></br>
    <label>
        <input type="radio" name="a1" value="site1" /> Site 1
    </label></br>
    <label>
        <input type="radio" name="a1" value="site2" /> Site 2
    </label></br>
</form>
<span class="r-text"><?php echo $selected;?></span>
<script>
    $('input[type=radio]').click(function(e) {//jQuery works on clicking radio box
        var value = $(this).val(); //Get the clicked checkbox value
        $('.r-text').html(value);
    });
</script>

这篇关于回声单选按钮值,而不使用PHP中的提交按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 22:27