PHP:单选按钮可见和隐藏按钮

PHP: Radiobutton visible and hide the button

本文关键字:隐藏 按钮 单选按钮 PHP      更新时间:2023-09-26

我有一个关于PHP"单选按钮"的问题。今天,我试图编码一个单选按钮,当我选择radiobutton1时,按钮提交应该是可见的,但当我选择radiobutton2时,按钮提交应该是不可见的。

My Problem is:

当我点击radiobutton2时,我想让submit按钮不可见,然后当我点击radiobutton1时,submit按钮应该可见。我试图做的代码不使用表单的行动。我已经搜索过这个了,但是都是关于form action的

下面是我的代码:

<html>
<body>
<Input type = 'Radio' name ='con' value= 'sample'>SAmple:
<Input type = 'Radio' name ='con' value= 'other'>Others:
<input type="submit" name="submit" value="Submit">  
</body>
</html>

提前感谢!

使用jQuery:

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
$(document).ready(function(){
    $('input[name=con]').change(function(){
        if($('input[name=con]:checked').val() == 'sample'){
            $('input[name=submit]').show();
        } else {
            $('input[name=submit]').hide();
        }
    });
});
</script>

在这里使用javascript。把你的HTML改成

  <html>
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
 <script>
   function show_but()
  {
         jQuery('#span_submit').show();
  }
  function hide_but()
  {
       jQuery('#span_submit').hide();
  }
 <script>
 <body>
   <Input type = 'Radio' name ='con' value= 'sample' onclick="show_but();">SAmple:
   <Input type = 'Radio' name ='con' value= 'other' onclick="hide_but();">Others:
  <span id="span_submit">
  <input type="submit" name="submit" value="Submit"> 
  </span> 
   </body>
   </html>

这是PHP无法做到的。这是JavaScript/jQuery

检查整个工作代码-

<html>
<body>
<Input type = 'Radio' name ='con' value= 'sample'>SAmple:
<Input type = 'Radio' name ='con' value= 'other'>Others:
<input type="submit" name="submit" value="Submit" style="display:none;">  
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
jQuery(document).ready(function(){
//jQuery('input[name=submit]').hide();
jQuery('input[name=con]').on('change',function(){
    if(jQuery(this).val() == 'sample')
        jQuery('input[name=submit]').show();
    else
        jQuery('input[name=submit]').hide();
});
});
</script>
</body>
</html>  

您也可以使用on方法,像这样-

jQuery('input[name=con]').on('change',function(){