简体   繁体   English

带有单选按钮事件事件的struts2

[英]struts2 with jquery radio button event issue

I have an issue with the radio button event handling using jquery. 我在使用jquery处理单选按钮事件时遇到问题。 I am new to JQuery but trying handle the html form elements events using JQuery. 我是JQuery的新手,但尝试使用JQuery处理html表单元素事件。 In the recent days I have added JQuery datepicker to my form which is working fine and now trying to add radio button and handle the checked and unchecked events of the radio button to hide and show some parts of the html elements( text fields, selection and other elements ). 最近几天,我在表单中添加了Jquery datepicker,它工作正常,现在尝试添加单选按钮并处理单选按钮的选中和未选中事件,以隐藏和显示html元素的某些部分(文本字段,选择和其他元素)。

Here is my code 这是我的代码

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
 <%@ taglib prefix="tags" uri="/struts-tags"%>
 <html>
 <head>
      <link rel="stylesheet" href="jquery/jquery-ui.css">
 </head>
 <body>
 <tags:form name="someForm" action="someAction" method="post">
  <tags:textfield key="someActionClass.transactionDate" id="datepicker"     label="Transaction Date" />
  <tags:radio list="#{'1':'One Time','2':'Recurring'}" id="tType" lable="Transaction Type" name="recurringOrOneTime"></tags:radio>


  <div class="recuType">
    <h1>this part should be hiden/show depending upon radio button events</h1>
  </div>

  <script src="jquery/external/jquery/jquery.js"></script>
  <script src="jquery/jquery-ui.js"></script>
  <script>

  $( "#datepicker" ).datepicker({
    inline: true
   });

    $("#tType").change(function(){
    alert('Radio Button event raised');
    });

   </script>
   </tags:form>
  </body>
  </html>

I have added the JQuery lib that I have downloaded for my JQuery datepicker in jquery directory as you can see from this link tag 我已经在jquery目录中添加了我为Jquery datepicker下载的JQuery库,如您从此链接标记中所见

  <link rel="stylesheet" href="jquery/jquery-ui.css">

so, When I click on the radio buttons no event is getting generated. 因此,当我单击单选按钮时,不会生成任何事件。 for the testing purpose I am trying to popup and alert message with the selected radio button value but no luck. 出于测试目的,我试图使用选定的单选按钮值弹出对话框并发出警报,但是没有运气。 I have gone through so many posts and examples and most of them are using html input tag and I want to use struts2 tag elements. 我已经阅读了很多帖子和示例,其中大多数都使用html input标签,并且我想使用struts2标签元素。

Is there any thing wrong I am doing with my code? 我的代码有什么问题吗? or missed any library to include? 或错过任何图书馆? please help 请帮忙

Use: 采用:

$("input[name='recurringOrOneTime']").change(function(){
        alert('Radio Button event raised');
        });

Instead of 代替

$("#tType").change(function(){
    alert('Radio Button event raised');
    });

id must be unique in a html document, you're assigning 2 radio buttons the same id. id在html文档中必须是唯一的,您要为2个单选按钮分配相同的id。

You can change two things: 您可以更改两件事:

  1. change the attribute id to class . 将属性id更改为class
  2. Bind the event on class name instead. 而是将事件绑定到类名。

Change 更改

id="tType"

to

class="tType"

and now bind the event: 现在绑定事件:

$(".tType").change(function(){
    alert('Radio Button event raised');
});

or even you can refer it with input[type="radio"], :radio etc: 甚至您也可以使用input[type="radio"], :radio等引用它:

$("form input[type='radio']").change(function(){ // or $("form :radio")
    alert('Radio Button event raised');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM