Javascript and only one Checkbox - undefined

后端 未结 8 1331
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-18 11:11
for (i = 0; i < document.checks.user.length; i++) //for all check boxes
{
    if (document.checks.user[i].checked == true )
    {
        document.checks.submit()         


        
8条回答
  •  长情又很酷
    2021-01-18 11:47

    Your question is somewhat confusing, since your javascript would obviously have to be inside a function called 'sub_delete' to be any use... someone with the mighty power to edit questions might improve the question by making that clearer...

    So the first issue you have to get around is the fact that for single checkbox, with a given name 'user', is not an array, and therefore has no defined length, but also if you try and access it as an array.. things get confused.. a full rewrite of your javascript function might look like this:

        function sub_delete{
            if (typeof document.checks.user.length === 'undefined') {
       /*then there is just one checkbox with the name 'user' no array*/
            if (document.checks.user.checked == true )
                                {
                                    document.checks.submit();
                                    return 0;
                                }   
        }else{
      /*then there is several checkboxs with the name 'user' making an array*/
            for(var i = 0, max = document.checks.user.length; i < max; i++){
                if (document.checks.user[i].checked == true )
                                {
                                    document.checks.submit();
                                    return 0;
                                }
    
            }
        }
        }//sub_delete end
    

    HTH, -FT

提交回复
热议问题