【问题标题】:Echo radio button value without using submit button in php在php中不使用提交按钮的情况下回显单选按钮值
【发布时间】:2017-12-11 04:32:26
【问题描述】:

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

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

我想要以下格式的输出

【问题讨论】:

  • 您想如何/在哪里回显所选值?如果您可以展示所需输出的示例,将会很有帮助。
  • 我已经添加了输出格式

标签: javascript php jquery html


【解决方案1】:

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

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

【讨论】:

  • 好的...我正在更新我的答案
【解决方案2】:

我不确定你想如何使用 PHP(!!!) 进行客户端操作,但这是一个 jquery 解决方案,用于在选中单选按钮时显示其值:: p>

$('#myform input[type=radio]').on('change', function(event) {
  var result = $(this).val();
  $('#result').html(result);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="myform" action="" method="post">
  <input type="radio" name="a1" value="home" /> home</br>
  <input type="radio" name="a1" value="site1" /> site1</br>
  <input type="radio" name="a1" value="site2" /> site2</br>
  <div id="result"></div>
</form>

【讨论】:

  • 我想要同样的方式。但是当我在我的代码中使用它时它不起作用
  • @user6582683 你为什么改问题?!!我已经回答了您的问题,也许您的代码由于其他原因无法正常工作。
【解决方案3】:

您需要在 javascript/jquery 中使用单选按钮的 onchange 事件。

PHP 运行在服务器端,而不是客户端,因此如果不向服务器发送客户端更改,它就无法输出基于它们的内容:您需要发送 $_POST$_GET通过提交表单或使用 AJAX 请求。不需要。

<div id="radioMsg"></div>
<form action="" method="post">
         <input type="radio" name="a1" value="home" onchange="showRadio()"  /> Home <?php echo ($selected == 'home' ? 'This was selected!' : '');?> </br>
         <input type="radio" name="a1" value="site1" onchange="showRadio()" /> Site 1 <?php echo ($selected == 'xyz' ? 'This was selected!' : '');?> </br>
         <input type="radio" name="a1" value="site2" onchange="showRadio()" /> Site 2 <?php echo ($selected == 'zbc' ? 'This was selected!' : '');?> </br>
</form>

同时在showRadio():

function showRadio(){
    var radioVal = $("input[name='a1']:checked").val();
    if(radioVal) {
        $( "#radioMsg" ).html("<p>"+radioVal+"</p>");
    }
}

我不确定您希望将更改后的按钮的值输出到哪里,因此我将其放入该 div 中作为示例。

在外部样式表中而不是内联中定义onchange 事件:

$( "input[name='a1']" ).change(function(){
    ... (showRadio's contents go here)
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-12
    • 1970-01-01
    • 1970-01-01
    • 2015-09-19
    • 1970-01-01
    • 2013-04-10
    • 2012-11-09
    相关资源
    最近更新 更多