简体   繁体   English

我正在使用struts2,询问的是单选按钮验证<s:radio>

[英]I'm using struts2,i'm asking about the radio button validation <s:radio>

<s:form action="creatingcv" method="post" onsubmit="return valider()" id="frmSaisie"> 

<s:radio name="cv.gender" label="Gender" list="{'Male','Female'}" id="Gender"/> 

</s:form> 

I used this javascript code but it didn't work 我使用了此javascript代码,但是没有用

function valider() { 

    frm=document.forms['frmSaisie']; 

    if(frm.elements['Gender'].value != "")  {       
        return true; 
    } 
    else { 
        alert("Please choose your gender"); 
        return false; 
    }
 } 

I tested with other components this javascript code and it worked well but with this one it does nothing. 我使用此javascript代码与其他组件进行了测试,并且效果很好,但与此代码无关。

I also tested frm.elements['Gender'].checked != false but it returns nothing. 我还测试了frm.elements['Gender'].checked != false但未返回任何内容。

For those who still want to use same 对于那些仍然想使用相同的

This was my radio button 这是我的单选按钮

<td><s:radio id="SME_RADIO" label="SME_RADIO" name="SME_RADIO"  list="#{'1':'Passed','2':'Failed'}"  value="smeVO.SME_RADIO"  labelposition="left" labelSeparator=":"/>

and this is how you can do validation,if nothing is selected it comes undefined that's way i kept if condition like that 这就是您可以进行验证的方式,如果什么都没有选择,那么在出现这种情况时,我将保持这种状态

radioValidation()
{
var nameCHK ="SME_RADIO";
     var selectedRadio = $('input[name='+nameCHK+']:radio:checked').val();
    if( selectedRadio !='1'||selectedRadio !='2' )
       {
         alert( "Please provide Some Radio!" );
         return false;
       }
}

: CRUD of database!! :数据库CRUD !! in that case i have to do several classes CreateAction UpdateAction..followed by CreateAction-validation.xml and UpdateAction-validation.xml...but here i faced a problem of objects creation and variables between update and read..the ID that i have to pass from form to form using didnt work.. i have 3 forms one for creating,listing,editing..to pass params from listing to editing...here i got the problems..i create an additional object databaseEdited and i get it from the other form..but the creating of the object is on the class Action that's why i have to work with one Action which contains all CRUD..even if i work with static...still the null java lang exception... 在那种情况下,我必须做几个类CreateAction UpdateAction ..之后是CreateAction-validation.xml和UpdateAction-validation.xml ...,但是在这里我遇到了对象创建以及更新和读取之间的变量的问题。必须使用不起作用的表格来传递。.我有3个表格用于创建,列出,编辑..将参数从清单传递到编辑...在这里我遇到了问题..我创建了一个附加的对象databaseEdited和i从另一种形式获取它。.但是对象的创建是在Action类上的,这就是为什么我必须使用一个包含所有CRUD的Action的原因。即使我使用static ...仍然是null的Java lang异常。 ..

HERE IS MY FORM DATABASE 这是我的表单数据库

<s:form  action="createDb" method="POST" onsubmit="return valider()" id="frmSaisie">
<s:if test="dbs.size">
<table border="1px">
    <tr>

        <td>ID</td>
        <td>DataBase</td>


        <td></td>
    </tr>
    <s:iterator value="dbs">
        <tr>

            <td> <s:property value="dbaId"/> </td>
            <td> <s:property value="dba"/></td>

 <td class="tab-td">
<s:url id="update" action="redirect">
<s:param name="databaseEdited.dba" value="dba"/>
<s:param name="databaseEdited.dbaId" value="dbaId"/>
</s:url> 
<s:a href="%{update}">Edit</s:a>
</td >

<td class="tab-td">
<s:url id="delete" action="deletingDb">
<s:param name="databaseDeleted.dbaId" value="dbaId"/>
</s:url>
<s:a href="%{delete}"> Delete</s:a>
</td > 

        </tr>
    </s:iterator>
</table>
</s:if>

My DATABASE EDITING form 我的数据库编辑表格

<s:form action="updatingDb" method="post" >
    <s:div>
<% as you see i get params from databaseEdited object but the id i ought to put it manually in action class --%>

        <s:textfield label="DataBase" name="databaseEdited.dba" id=DB />
    </s:div>
    <br>
  <s:submit value="Submit" />

     <s:reset key="Cancel"/> 

        </s:form>   

In my action class: 在我的动作课中:

package com.consoft.cv.action;

import java.util.ArrayList;
import java.util.List;



import javax.naming.Context;
import javax.naming.InitialContext;

import com.consoft.cv.entity.*;

import com.consoft.cv.model.*;

import com.opensymphony.xwork2.ActionSupport;

public class DBAction extends ActionSupport {


private static final long serialVersionUID = 1L;
private DBMBLocal DBEjb;
private DB database;
private DB databaseEdited;
private DB databaseDeleted;
private List<DB> dbs;
private static int uid;

public DBAction(){
    try {
        Context context=new InitialContext();
         DBEjb=(DBMBLocal) ( context.lookup("DBMB/local"));


        dbs=new ArrayList<DB>();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

public String findAll() {
    this.dbs=DBEjb.findAll();
    return "success";
}
public String create()
{

    DBEjb.create(database);
    return "success";

}
public String update()
{//the UID got from redirect method
    databaseEdited.setDbaId(uid);

DBEjb.update(databaseEdited);
    return "success";

}
public String delete()
{

    DBEjb.delete(databaseDeleted);
    return "success";

}
public String redirect()
{
    //this is the key cause HERE I CAN KEEP PASS THE ID TO Update method
     **uid=databaseEdited.getDbaId();** 
    return "success";
}

of course there are getters and setters 当然有吸气剂和吸气剂

BECAUSE OF THESE PROBLEMS I COULD NOT WORK WITH STRUTS2 VALIDATION 由于这些问题,我无法使用STRUTS2验证

Think that <s:radio> tag will be replaced with something similar to this: 认为<s:radio>标签将被替换为以下内容:

 <input type="radio" id="Gender" value="Male"/>
 <input type="radio" id="Gender" value="Female"/>

So then, frm.elements['Gender'].value != "" will not work property since there are two elements that matches that. 因此, frm.elements['Gender'].value != ""将不起作用,因为有两个元素与此匹配。 Check each element separately: 分别检查每个元素:

function valider() {
  frm=document.forms['frmSaisie'];

  if(frm.elements[0].checked == true || frm.elements[1].checked == true)  {       
    return true;
  }
  else {
    alert("Please choose your gender");
    return false;
  }
}

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

相关问题 如何使用JavaScript取消选中Struts2中的单选按钮 - How to uncheck a radio button in struts2 using javascript 带有单选按钮事件事件的struts2 - struts2 with jquery radio button event issue 我正在尝试显示由所选单选按钮确定的图像 - I'm attempting to display an image determined by the radio button that is chosen 我正在尝试动态创建javascript Dom单选按钮和属性 - I'm trying to create a javascript Dom radio button and attribute dynamically 我想把一个<button>里面</button><button><input type=“radio”></button><button>是<label>为手机</label></button> - I'm trying to put a <button> inside an <input type=“radio”>'s <label> for mobile 单选按钮对象不支持选中的方法,但我使用的是以前曾使用过的语法 - Radio button object doesn't support method checked, but I'm using syntax that has worked before struts2单选按钮id属性值正在动态更改 - struts2 radio button id attribute value is changing dynamically 面对验证是当我选择单选按钮时 - facing in validation the is when i select radio button 我在选择单选按钮时尝试提交表单但未成功 javascript - I'm trying to submit a form when selecting a radio button and not successful javascript 当我不使用提交按钮时,不会显示jQuery验证错误 - jQuery validation errors are not shown when I'm not using a submit button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM