douke3007 2012-05-09 22:31
浏览 23
已采纳

是否可以使用PHP添加事件监听器?

I'm creating a survey form using Smarty templates in which I want to attach an event with radio buttons of a particular question such that it pops-up a message when any one of them is clicked. So I want to add a click/change event listener on them but dont know how to do it using php. As I do not know the radio button id in advance (they are generated on the fly), I'm not sure if I can use Javascript here. Any solutions/suggestions?

Google search returns all sort of answers for adding event listeners for android and javascript which are of no use!

  • 写回答

4条回答 默认 最新

  • doujingao6210 2012-05-09 23:06
    关注

    Simply add a class to the radio buttons, then using JavaScript/JQuery, you could add event listeners to the inputs as needed.

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="myjsfile.js"></script>
    <input type="radio" class="clickyradio" />
    <input type="radio" class="clickyradio" />
    <input type="radio" class="clickyradio" />
    

    now in the file myjsfile.js

    $(document).ready(function() {
       $('.clickyradio').click(function() {
          // whatever you want to happen when it gets clicked
          // "this" keyword will refer to the input element that got clicked
    
       });
    });
    

    if you need the click to communicate something to the server, put an ajax call within the click event handler.

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

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效