Javascript issue with checkbox array when only one item is selected
I have a form with checkboxes, these checkboxes store there value in an array when checked this way:
<input type='checkbox' name='listaction[]' value='2010102909103530'>
On submit I check which checkboxes were checked and I do something with it.
My problem occurs when only one item is selected then listaction isn't an array but just a string ...
How do I handle this ?
It starts all with the submit button that firesup desubmit()
These are the handling functions:
function desubmit()
{
if(get_args()==false) {alert("U hebt geen treinen geselecteerd!");return false;}
if(labelling(true)) return false;
}
function Check(chk)
{
for (i=0; i < chk.length; i++) chk[i].checked=document.ListActionForm.Check_ctr.checked ;
}
function labelling(s)
{
notrains="U hebt geen treinen geselecteerd!"
selectval=document.ListActionForm.la.options[document.ListActionForm.la.selectedIndex].value;
if(selectval=='exportoptions') {popUpWin('form.php?exportconfig=1','console3',470,470);}
else if(selectval=='newlabel'&&!s) {jPrompt('Nieuwe Lijst:','Default', 'Maak nieuwe lijst (Max 20 karakters)', function(r) {if(r) {if(r.length>20){alert("Gekozen naam lijst mag maximum 20 tekens lang zijn (Overige tekens worden automatisch verwijderd)");r=r.substr(0,20);};document.ListActionForm.newlabel.value=r;document.getElementById('shownewlabel').innerHTML='[ Nieuwe Lijst: '+r+' ]';}});document.getElementById('popup_prompt').maxlength=5;}
else if(selectval=='export:pdf') {if(arg=get_args()) get_page('/PDF/pdf.php','ids',arg);else alert(notrains);}
else if(selectval=='export:csv') {if(arg=get_args())get_page('?export=csv','ids',arg);else alert(notrains);}
else if(selectval=='export:xlsapp') {if(arg=get_args())get_page('?export=excelvbs','ids',arg);else alert(notrains);}
else if(selectval=='export:xlsapptxt') {if(arg=get_args())get_page('?export=excelvbstxt','ids',arg);else alert(notrains);}
else return false;
return true;
}
function get_args()
{
s=chkboxa2str(document.ListActionForm['listaction[]']);
if(s.length<8)return false;
else return s;
}
function chkboxa2str(chkbox_a) {
var list = "";
for(var i = 0; i < chkbox_a.length; i++){ if(chkb开发者_开发问答ox_a[i].checked) { list += chkbox_a[i].value + " "; } }
return list;
}
You can check if it's string and handle it in other way. Use instanceOf for this:
if(listaction instanceOf String) ...
I'm now completely lost ...
I added this function to test the type off my array
function test (obj) {
var type = typeof obj;
if (type == 'object') {
if (obj.getDate) return 'Date';
if (obj.split) return 'String';
return object;
}
return type;
}
and I putted a debug line with this function in get_args function like this: function get_args() { test (document.ListActionForm['listaction[]']); s=chkboxa2str(document.ListActionForm['listaction[]']); if(s.length<8)return false; else return s; }
And suddenly the array is only recognised when there is only one element selected (I suppose as string) but not anymore when multiple checkboxes are selected
The test line doesnt even do anything ....
Is this a bug ?
To make it much more simple I've createad a fully working html page where the bug represents itself.
To simulate the problem just run it once with two html input checkbox elements -> check one checkbox and press submit -> it works -> Now remove one input checkbox field and check the remaining checkbox & submit -> Bamm doesn't work...
<html>
<head>
<script>
function Check(chk)
{
for (i=0; i < chk.length; i++) chk[i].checked=document.ListActionForm.Check_ctr.checked ;
}
function desubmit()
{
if(get_args()==false) {alert("$t_notrains_selected");return false;}
if(labelling(true)) return false;
}
function labelling(s) {return true;}
function get_args()
{
s=chkboxa2str(document.ListActionForm['listaction[]']);
if(s)alert(s);
if(s.length<8)return false;
else return s;
}
function chkboxa2str(chkbox_a) {
var list = "";
for(var i = 0; i < chkbox_a.length; i++){ if(chkbox_a[i].checked) { list += chkbox_a[i].value + " "; } }
return list;
}
</script>
</head>
<body>
<form action="?h=1296078874" method="post" name="ListActionForm" onsubmit="return desubmit()">
<input type='checkbox' name='listaction[]' value='2010102909103530'> Testbox 1<br>
<input type='checkbox' name='listaction[]' value='2010102909103532'> Testbox 2<br>
<input type="Submit" name="Submit" value="Versturen" >
</form>
</body>
</html>
精彩评论