NeWorld

Dirbam su radiobutton’ais

by neworld on Bir.02, 2007, under Javascript, Programavimas, WEB programavimas

Kartais tenka javascriptu pagalbą pasiimti radiobutton reikšmę. Bet standartiniu būdų jos pasiimti neina

[code lang="javascript"]var value=document.getElementById(element).value);[/code]

Todėl tenka rašyti šiek tiek sudėtingesnę funkciją:

[code lang="javascript"]function getCheckedValue(radioObj) {
if(!radioObj)
return "";
var radioLength = radioObj.length;
if(radioLength == undefined)
if(radioObj.checked)
return radioObj.value;
else
return "";
for(var i = 0; i < radioLength; i++) {
if(radioObj[i].checked) {
return radioObj[i].value;
}
}
return "";
}
function setCheckedValue(radioObj, newValue) {
if(!radioObj)
return;
var radioLength = radioObj.length;
if(radioLength == undefined) {
radioObj.checked = (radioObj.value == newValue.toString());
return;
}
for(var i = 0; i < radioLength; i++) {
radioObj[i].checked = false;
if(radioObj[i].value == newValue.toString()) {
radioObj[i].checked = true;
}
}
}[/code]

naudojimo pavizdys:

[code lang="javascript"]var a=getCheckedValue(document.forms['form1'].elements['myradio']);[/code]


Comments are closed.

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!