drktvjp713333 2012-11-28 07:51 采纳率: 0%
浏览 101
已采纳

PHP - 选择单选按钮后直接显示消息

I am using a PHP script, but say I had two radio buttons, right?

How could I actually execute code, such as (main intention | display a messagebox) upon selection of one or the other?

Say I had a radio button named RadioButton1, Once checked/selected, a message box would appear saying RadioButton1 Selected?

Is this possible through PHP alone? Or do I need to integrate an html page which posts to the PHP page?

  • 写回答

5条回答 默认 最新

  • dounai7148 2012-11-28 08:00
    关注

    Use Javascript for client side interaction like that. The code below listens for the onchange event and shows an alert().

    jsFiddle Demo

    <input type="radio" name="myradio" value="RadioButton1" />
    <input type="radio" name="myradio" value="RadioButton2" />
    <input type="radio" name="myradio" value="RadioButton3" />
    
    <script>
    ​window.onload = function()
    {
        var radios = document.getElementsByName("myradio");
         for(var i=0; i<radios.length; i++)
         {
            radios[i].onchange = function()
            {
                if(this.checked)
                {
                    alert(this.value + " selected");
                }
            }
         }             
    }​
    </script>
    

    The first 3 lines are the radio buttons HTML. After that we have the <script> tag which denotes Javascript code. The Javascript is adding some code to the onload event, which simply means: execute this code when the page is loaded. Next we get all of the radio button elements into an array called radios - for that we use getElementsByName() passing the radio button group name which is myradio. Next we loop through each radio button in the array and assign an onchange handler, which means: execute this code when each radio button is changed. Within that, we check if the radio button is checked and if it is, we show the alert, showing the radio button's value which will be RadioButton1, RadioButton2, RadioButton3.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?