views:

682

answers:

2

Hi, thank you for your time.

I am trying to use 2 Jquery solutions on the one form, but I am unable to get the desired result after working on it for several hours.

The first jquery solution enables a better look and feel http://www.envirotalk.com.au/jquery/jquery-checkbox.htm

The second jquery solution enables a limit to be set on the number of checkboxes selected. http://www.envirotalk.com.au/jquery/jquery-field.htm

This is my attempt at combining the two. http://www.envirotalk.com.au/jquery/combined.htm

I believe the problem relates to this line of code, but I cannot be certain.

setLimitSelection();

The outcome I am trying to achieve.

  • Look and feel; jquery-checkbox
  • Limit to the number of checkboxes one can select; jquery-field
  • The error message; jquery-field
  • Clearing the extra field value once exceeded limit; jquery-field.

If someone has the time to take a look and help me, that would be greatly appreciated.

Regards

zeroanarchy

A: 

The two plug-ins are probably conflicting with each other, since the code that you're using is the same as the ones given in the individual examples. For limiting checkboxes that can be selected, you can try this:

function setLimitSelection(){
 $("input[type=checkbox]").click(function(){
  var checkboxes_to_allow = 3;
  if($('input[type=checkbox]:checked').length == (checkboxes_to_allow+1)){
   //show error message
   return false;
  } else {
   //clear message
  }
 });
}

I think the code is clear enough to not need comments?

aditya
A: 

Hello I think you can solve the problem with a small status variable like checkfield:

var checkfield = false;

$("input[name ='name']").limitSelection({

limit: 3,
onfailure: function (n){
checkfield = true;
return false;
}
}).click(function (){
if (field6error)
{   
$(this).checkBox('changeCheckStatus', false);
checkfield = false;
alert("Not more than 3 values!")
return false;
}});