How can I capture checkbox & radiobutton items in jsp page(plz code too)Thanks

Showing Answers 1 - 1 of 1 Answers

Kamal Kr. Agrawal

  • Aug 2nd, 2006
 

For Radio Button

You can use javascript to retrieve the value of the radio button.Save this value in a hidden field in the same form and in the next page just get it by getparameter method.

This is the javascript code to get the value of the radio button which is checked.

function getValue()
{  
 var a=0;
      for(i=0;i<window.document.form1.elements.length;i++)
     {
    if (window.document.form1.elements[i].name=="name")
                if (window.document.form1.elements[i].checked==true)
                    { 
   alert(window.document.form1.elements[i].value + "  is clicked" );
   a=1;
   
  } 
  }
 if(a==0)
 { alert("Please Select A Radio Button"); 
    return false;
 }
 
}

For Checkbox

For checkbos also you can use same method as for radiobutton.

Here i am giving you another function for the same.

function check()
{

   var count=0;
  if(oneCheckedE("number"))
  { 
   for (var i=0;i < document.form1.elements.length;i++)
   {
    var e = document.form1.elements[i];
    
    if (e.type == "checkbox" )
    {

     if(e.checked)
     {
      
      var d = i+1;
      var no=document.form1.elements[d].value;
      
      alert("Number Clicked is "+no);

      
      break;
      
     }
     count++;

    }
   }

  }   
   
}

function oneCheckedE(chkboxname)
  {
   var rowcount=0;
   var numberOfCheckboxs=0;
   for (var i=0;i < document.form1.elements.length;i++)
   {
    
    var e = document.form1.elements[i];
    
    
    if (e.type == "checkbox" && e.name == chkboxname)
    {
     var chkQty=e.name;
     
     if(e.checked)
     {
      
      rowcount++;
     }
     numberOfCheckboxs++;
    }
    
   }
   numberofRows=numberOfCheckboxs;
   if(rowcount<=0 || rowcount > 1)
   {
    alert("Please Select One Checkbox");
    
    return false;
   }else
   {
    return true;
   }
   
   
  
  }

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions