How to check if a checkbox in a checkbox array is checked with jquery

How to check if a checkbox in a checkbox array is checked with jquery

I have a html table with a column of text boxes (mileage), all of them are disabled when the page loads, and I need that when user clicks on a check box, the text box for that column should be enabled and become a required field, and then  if  the user unchecks the checkbox, the textbox on that row must be disabled and the required class removed.

I already have the function to enable and disable the text boxes (column mileage) when the user clicks on the corresponding   checkbox, but it is written in javascript.However, for some reason it only works in IE).
Here is the html code

 
    
      
         " onchange="enableTextField(this)" />
      
      
      
      " value="" disabled="true"/>
     
                         


And the javascript code:
function enableTextField(r)
    {   var node = r.parentNode;
        while( node && node.tagName !== 'TR' ) {
            node = node.parentNode;
        }
        var i=node.rowIndex;

        if(document.form1.selectItems[i-1].checked)
        {   

            document.getElementById('mileage_' + document.form1.selectItems[i-1].value).disabled=false;
        }
        else
        {

            document.getElementById('mileage_' + document.form1.selectItems[i-1].value).value="";
            document.getElementById('mileage_' + document.form1.selectItems[i-1].value).disabled=true;
        }


    }

Now, I know that in order to add or remove dynamically validation rules I have to use: addClass('required');  o removeClass('required') ,but I don't know how to detect whether a  check box is selected or not, and based on that ,enable or disable the text box for that row.
I really hope you can help me out with this.

Solutions/Answers:

Answer 1:

Put this in <head> or in a .js file which is included in <head>.

$(document).ready(function() {
    $("input[name=selectItems]").change(function() {
        $(this).closest("tr").find("input[name^=mileage]").attr("disabled", !this.checked);
    });
});

Here’s a live demo.

Answer 2:

To check if a checkbox is checked or not in jquery you would do $('#CHECKBOX_ID').is(':checked') that will return a boolean you can use in an if statement

Answer 3:

Add an id to the checkbox, call the checkbox through the id, then check for the boolean value of the checkbox.

<!DOCTYPE html>  
<html> 
<head> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>
<body>
<input id="sam" type="checkbox" name="vehicle" value="Bike" />
</body>
</html>

This will return true/false

$('#sam').is(':checked')

Answer 4:

Something like this should work:

$("input[name='selectItems']").change(function() {

    var $el = $(this);

    if(this.checked) {
        // checkbox checked.
        $el.parents("tr").find("input[name^='mileage_']").atrr("enabled", "enabled");
    } else if(!this.checked) {
        // checkbox NOT checked.
        $el.parents("tr").find("input[name^='mileage_']").atrr("disabled", "disabled");
    }

});

Our Awesome Free Tools

References

Can you make a jQuery call to WCF service using SOAP and WSHttpBinding?

Can you make a jQuery call to WCF service using SOAP and WSHttpBinding?

Can i make a JQuery call to WCF service using SOAP and WSHttpBinding?
If so, how? (any examples on the web?) If not, why not?
As you can see from this post, i cannot get it to work.
I need to be able to use WSHttpBinding so i can support SAML and WS-Security. It is my understanding that basicHTTPBinding does not support ws-Security.
All other examples on SO either use json (which i can get to work easy) or basicHttpBinding.
I am required to use SOAP and ws-Security to comply with OGC 07-118r8 standards.

Solutions/Answers:

Answer 1:

With something like WS-* and the complexity of WCF, wouldn’t it be easier to create a simple jQuery async callback to another page on your site that can leverage the .NET built-in WS-Security Libraries? From there you can fire off a call to the WCF endpoint, process the result, and return it back to the jQuery function as an easily readable response.

I found some articles that mention what you’re trying to do. It sounds like you would essentially have to write your own WS-Security jQuery library before this would work straight from JavaScript (and I imagine that’s probably impractical for you):

“A service exposed with “wsHttpBinding” binding implements WS-Security of WS-* family of web service specifications. As the binding uses security, the request will be rejected. The service cannot be called without implementing WS-Security at JQuery end. So, in order to call the service method, we have to compromise with the security.”

http://www.codeproject.com/Articles/311908/Calling-WCF-service-exposed-with-different-binding

“…wsHttpBinding, which is a SOAP binding that won’t work with AJAX.”

http://www.west-wind.com/presentations/jquery/jquerypart2.aspx

Answer 2:

No way you can implement WS* in JavaScript!

Best option is to create another binding on the server and expose it as basic HTTP or even better as WCF REST.

Answer 3:

Should be possible in theory. I’m assuming for the moment you’ve figured out how to get your SAML token and deal with WS-Security. WSHttpBinding defaults to soap 1.2, so either look at the http binding spec to figure out how to construct the request using jquery: http://www.w3.org/TR/soap12-part2/#soapinhttp or set EnvelopeVersion.Soap11 on WSHttpBinding. good luck!

Our Awesome Free Tools

References