开发者

Checkbox validation - at least one selected

I have number of checkboxes and another checkbox for "Select All"

I want to check if the user has selected at least one checkbox. Need modification in javascript

<script language="Javascript">

function doSubmit(){
function check_checkboxes() 
{ 
checked=false;
var c = document.getElementsByTagName('INPUT'); 
for (var i = 1; i < c.length; i++) 
{ 
    if (c[i].type == 'checkbox') 
    { 
    if (c[i].checked) {
    return true} 
    else {alert("Please identify what warehouses comply:");  }
        } 
    } //if I place my struts action here..its not working?
}    
document.holiDay.command.value= 'addingApp'; //My Struts ac开发者_Go百科tion if something checked. 
document.holiDay.submit(); 
}    


var all=document.getElementById('holiDay');

In HTML IDs should be unique, so getElementById will only return 1 element. Perhaps you could try getElementsByTagName - http://msdn.microsoft.com/en-us/library/ms536439(VS.85).aspx ?

Something like...

function check_checkboxes()
{
  var c = document.getElementsByTagName('input');
  for (var i = 0; i < c.length; i++)
  {
    if (c[i].type == 'checkbox')
    {
       if (c[i].checked) {return true}
    }
  }
  return false;
}

and change your Validate function to...

function Validate()
{
    if(!check_checkboxes())
    {
        alert("Please identify what warehouses comply:");  
        return false;
    }
    return true;
}


Select at least one check box using jqQery. Try the following code.

$('input[type="checkbox"][name="class"]').on('change', function () {
    var getArrVal = $('input[type="checkbox"][name="class"]:checked').map(function () {
        return this.value;
    }).toArray();

    if (getArrVal.length) {
        //execute the code
    } else {
        $(this).prop("checked", true);
        alert("Select at least one column");
        return false;
    }
    ;
});


 (function() {
  for(x in $ = document.getElementsByTagName("input"))
   with($[x])
    return (type == "checkbox" ? checked == true : 0)
 })
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜