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 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号