1

I want to add attribute checked=true with multiple inputs like this:

<form id="students" method="post">
                  <div class="row">
                      <input id="aa" name="a[]" value="Smith" type="text" class="a1" >
                      <input id="bb" name="b[]" value="Alen" type="text" class="b1" >
                      <input id="save" name="save[]" value="" type="checkbox" class="ab" disabled="disabled" >
                  </div>
                  <div class="row">
                      <input id="aa" name="a[]" value="" type="text" class="a1"  >
                      <input id="bb" name="b[]" value="" type="text" class="b1" >
                      <input id="save" name="save[]" value="" type="checkbox" class="ab"  >
                  </div>
                  <div class="row">
                      <input id="aa" name="a[]" value="Bill" type="text" class="a1"   >
                      <input id="bb" name="b[]" value="Mark" type="text" class="b1" >
                      <input id="save" name="save[]" value="" type="checkbox" class="ab" >
                  </div>
                  <div class="row">
                      <input id="aa" name="a[]" value="" type="text" class="a1"  >
                      <input id="bb" name="b[]" value="" type="text" class="b1" >
                      <input id="save" name="save[]" value="" type="checkbox" class="ab" >
                  </div>
                  <div class="row">
                      <input id="aa" name="a[]" value="Kell" type="text" class="a1"  >
                      <input id="bb" name="b[]" value="Keith" type="text" class="b1" >
                      <input id="save" name="save[]" value="" type="checkbox" class="ab" >
                  </div>
            <input type="submit" value="submit" id="submitbutton" class="insert" onclick="checkform()"/>
</form>

And each line input has no value I add attribute checked=false of checkbox. How I do. Who can help me? thanks.

This is javaScript :

<script type="text/javascript">
 function checkform() {
   var myForm = document.forms.students;
   var myControls = myForm.elements['a[]'];
   for (var i = 0; i < myControls.length; i++) {
    if(myControls[i].value==""){
        $(".ab").attr("checked", true); //check input had value then 
        }
  }
 }
</script>
HTT
  • 41
  • 1
  • 9
  • 2
    You are missing a `.` in this line `$(".ab")attr("checked", true);` It should rather be `$(".ab").attr("checked", true);` – Shekhar Chikara Apr 08 '16 at 15:18
  • ah. Sorry. I put up is not enough – HTT Apr 08 '16 at 15:26
  • @HTT **Do not use an id on more than one element** A duplicated id will cause unexpected behavior. You have 5 sets of `id='aa'`, `id='bb'`, and `id='save'`. Change them to a class or place a unique number or letter on their ids – zer00ne Apr 08 '16 at 16:24
  • Are you checking only inputs that are `name="a[]"`? or do want to check `name="b[]"` as well? – zer00ne Apr 08 '16 at 16:31

2 Answers2

0

Your question says: "... I add attribute checked=false..."

But your code says:

$(".ab").attr("checked",true); //not false like the question says

Please tell me that's not it ;)

Leon
  • 301
  • 1
  • 6
  • What I mean is when I press the submit, then check if the line is going to have value add attr("checked",true). otherwise add attr("checked",false) – HTT Apr 08 '16 at 16:03
  • Have you tried prop instead of attr? http://stackoverflow.com/questions/5874652/prop-vs-attr – Leon Apr 08 '16 at 16:13
  • The checked property is a bit special. If a checkbox has it, the checkbox is checked. Setting it to false has no effect. If you want an unchecked checkbox, the property should not be added to it. See here: http://stackoverflow.com/questions/10907454/i-have-set-the-html-checkbox-the-checked-property-to-false-but-it-still-checked – Leon Apr 08 '16 at 16:29
0

What about something like this? Check out this JSFiddle.

You can swap them around if I misunderstood and you want inputs with something in them to be checked and not vice versa.

In the fiddle I changed it so that the button click is handled by jquery instead of the onClick() attribute of the input.

function checkform() {
   var myForm = document.forms.students;
   $(myForm).find('input[name="a[]"], input[name="b[]"]').filter(function(){
        if(!$.trim(this.value)){
            $(this).siblings('.ab').prop('checked', true)
        }else{
            $(this).siblings('.ab').prop('checked', false)
        }
   });
 }

I also updated it to check both a[] and b[].

Additionally, I removed the myControls variable since you don't need it here.

Jeramiah Harland
  • 854
  • 7
  • 15