Monday, July 4, 2011

multiple radio button and multiple select problem with javascript

form name="choose_seats" action="" method="post" onsubmit="return valid();"

//this is in loop
select name="dep_boarding_point[$i]"
option value=""--Select--/option
php loop goes here
/select
//end

input type="radio" name="select_trip" id="select_trip" value="$i"
close form


this is javascript



function checkRadio()
{
var x=document.forms["choose_seats"]["select_trip"];
var uncheck = 0;

var objLength = x.length;

//this condition is for if there is one radio button
if(objLength == undefined){
if(x.checked)
{
return x.value;
}
}
for(i=0;i {
if(x[i].checked == true)
{
var chekradioval = x[i].value;
return chekradioval;
}

}
}


function valid()
{
var frmobj = document.forms["choose_seats"];
checkedRadioVal = checkRadio();

//this condition is for if there is one radio button
if(checkedRadioVal == undefined )
{
alert("select trip!");
return false;
}

if(checkedRadioVal > 0)
{
var checkedRadioPickupPoint = frmobj.elements['dep_boarding_point['+checkedRadioVal+']']; //note here we used elements because select come with multiple time

if(checkedRadioPickupPoint.selectedIndex == 0)
{
alert("select pickup point!");
return false;
}

}
return true;
}
Read More »