1

How do I enable the button if one or more checkbox is checked and if the select option of the checked checkbox is not equal to "no action"? Treating it as a row of table with checkbox being first element and select as last element?

This is how I am checking checkbox values.

<script>
  var checkboxes = $("input[type='checkbox']"),
      submitButt = $("input[type='submit']");


  submitButt.attr("disabled", "disabled");
  checkboxes.click(function() {
    submitButt.attr("disabled", !checkboxes.is(":checked"));
  });
</script>

//dynamically generating rows

for ws in my_zones:
   html_output += \
        ''''<tr><td><input type="checkbox" name="checkboxes" value="%s"/></td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>
            <select name="action">
                <option value='no action'>Choose Action</option>
                <option value='scan'>Reboot</option>
                <option value='swap'>Rebuild</option>
                <option value='terminate'>Terminate</option>

             </select></td></tr>''' \
        % (.......)

enter image description here

3
  • Do you mean custom select attribute? Commented Apr 19, 2015 at 1:35
  • i meant select tag in html. Commented Apr 19, 2015 at 1:39
  • I posted a possible solution, is that what you wanted? Commented Apr 19, 2015 at 2:04

3 Answers 3

2

At each check state change, verify what you want: if there at least one checkbox checked and if the "no action" checkbox is not checked. Then you can enable the submit button, or otherwise disable it.

function updateSubmitButtonState(){
   
  var enableableLineCount= 0;
  $("tr").each(function(){
    if( isEnableableLine( this ) )
      enableableLineCount++;
  });
  
  if( enableableLineCount > 0 )
       $('[type="submit"]').removeAttr("disabled");
    else
       $('[type="submit"]').attr("disabled","disabled");
  
  
  function isEnableableLine( tr ){
  
    if( 
        $("input[type='checkbox']", tr).is(":checked") && 
        $("select option[value='no-action']:selected", tr ).length == 0 
    )
      return true;
    else
      return false;
  }
}

$("input[type='checkbox']").on("click",updateSubmitButtonState );
$("select").on("change",updateSubmitButtonState );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table>
  <tr>
    <td>
     <input type="checkbox" name="checkbox-group">
    </td>
    <td>
      <select>
        <option value="option1">option 1 </option>
        <option value="option2">option 2 </option>
        <option value="no-action">no-action</option>
      </select>
    </td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" name="checkbox-group">
    </td>
    <td>
      <select>
        <option value="option1">option 1 </option>
        <option value="option2">option 2 </option>
        <option value="no-action">no-action</option>
      </select>
    </td>
  </tr>
</table>

<input type="submit" disabled="disabled" value="Submit" id="submit" />

edit

I have adapted the snippet to fit the goals described in coments. Now, at each change, the whole table is parsed. If there not hundred of line, it could be sufficient. But it is not very optimised.

Sign up to request clarification or add additional context in comments.

10 Comments

thanks for the answer but last element is a select option not a checkbox. how to manage that?
i have updated my question with the snapshot and also with the structure of html. Since I am dynamically generating rows of the table, I am not thinking of using "id". Can we play without the "id"?
I tried to fit your example, but note that providing code may be more efficient.
As I understand, if there just one select which has an "no action" value, the submit button is disabled, is that right?
Because, you may be want that it is only if the select value of the checked line have a "no action" value which disable the submit button.
|
1

https://jsfiddle.net/gzrv8v3q/13/

var checkboxes = $("input[type='checkbox']"),
submitButt = $("input[type='submit']");


submitButt.attr("disabled", "disabled");


$('#form1').change(function() {
    var disable = true
    if ( $('#select').val() != "no-action") {
        checkboxes.each(function() {
            if ($(this).is(":checked")) {
                disable = false
            }
        })
    }
    submitButt.attr('disabled', disable);
})

2 Comments

if you have only one checkbox don't use each function
each row will have on checkbox..i have uploaded an image for more clarity.
0

The disabled attr must be deleted. Even it existing makes it disabled. I use: https://api.jquery.com/removeAttr/

Also, your code will not add disabled back again if the checkbox is unchecked, also clarify if that is part of your question.

2 Comments

but i checked, checking and unchecking checkboxes and it is working fine..but then how to "&&" "no action" select option with this.. just started with jquery.
That was the start of your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.