简体   繁体   中英

To disable the selective check box on page load jquery

I have a problem and search for this a while but , didnt get any soultion.

i have a page like:

 <!doctype html>
    <html>
    <body>
    <table id='mytable'>
    <tr>
    <thead>
    <th><input type="checkbox" id="action-toggle" /></th>
    <th>Institute</th>
    <th>Version</th>
    </tr>
    </thead>
    <tbody>
    <tr class="row1"><td><input type="checkbox" class="action-select" value="119" name="_selected_action" /></td>
    <td>ABCD Engineering College</td>
    <td>1.0</td></tr>
    <tr class="row1"><td><input type="checkbox" class="action-select" value="119" name="_selected_action" /></td>
    <td>EFGH Engineering College</td>
    <td>2.0</td></tr>
    <tr class="row1"><td><input type="checkbox" class="action-select" value="119" name="_selected_action" /></td>
    <td>IJKL Engineering College</td>
    <td>1.0</td></tr>
    </tbody>
    </body>
    </html>

    jQuery(document).ready(function($){
        jQuery("#result_list tbody tr").each(function(index,el){
                    val=$(el).text();
                    (if val=='1.0'){
                    $("el:contains('1.0')").closest("td").find("input.action-select").attr('disabled','disabled');
                    }
    });
    });



    jQuery(document).ready(function($){
        jQuery("#result_list tbody tr").each(function(index,el1){
                    $(el1).each(function(idex,el2){
                    val=$(el2).text();
                    (if val=='1.0'){
                    $("el:contains('1.0')").closest("td").find("input.action-select").attr('disabled','disabled');
                    }
    });
    });

Now i want in tbody where there is version 1.0 its respective check box should get disable on page load. I applied the .each function and two .each function as well but did not get the desired result. I would be really very helpful if could answer this .Thanks in Advance.

jQuery(document).ready(function($){
    jQuery("#mytable tr").each(function(){
       var version=$(this).find('td:last').text();
       if (version === "1.0"){
           $(this).find("input[type='checkbox']").attr('disabled','disabled');
         } 
    });     
}); 

fiddle : http://jsfiddle.net/R27wq/1/

also include </table> in your html code

Try this: http://jsfiddle.net/nvn6J/

$('#mytable tbody tr').each(function() {

    var row = $(this);

    if($(this).find('td:last').text() == '1.0') {
       $(row).find('input[name="_selected_action"]').prop('disabled', 'disabled');
    }

});

This works, but I believe this is best solved by disabling the options from the back end. I have no idea what your back-end code looks like or even if you're using PHP, but this is an example of what I'm talking about:

$counter = 1;
foreach($colleges_array as $College) {
    echo '
        <tr class="row' . $counter . '">
            <td><input type="checkbox" class="action-select" value="' . $College->id . '" name="_selected_action" ' . ($College->version == '1.0' ? ' disabled="disabled' : '') . ' /></td>
            <td>' . $College->name . '</td>
            <td>' . $College->version . '</td>
        </tr>';

     $counter++;
}

Got it: http://jsfiddle.net/QUVjZ/1/

$("#mytable tr td").each(function(index, el1) {

    val = $(el1).html();
    if (val == '1.0') {
        $(el1).parent("tr").find("td:eq(0) input[type='checkbox']").attr("disabled", "disabled");
    }

});

A good implementation would be to use custom data attributes available in HTML.

Set the data attribute to the input boxes

    <input type="checkbox" class="action-select" value="119" 
name="_selected_action" data-version='1.0' />

Notice a new attribute added to the checkbox data-version='1.0'

Your Table will look like the following:

    <html>
<body>
<table id='mytable'>
    <tr>
        <thead>
        <th><input type="checkbox" id="action-toggle"/></th>
        <th>Institute</th>
        <th>Version</th>
    </tr>
    </thead>
    <tbody>
    <tr class="row1">
        <td><input type="checkbox" class="action-select" value="119" name="_selected_action" data-version="1.0"/></td>
        <td>ABCD Engineering College</td>
        <td>1.0</td>
    </tr>
    <tr class="row1">
        <td><input type="checkbox" class="action-select" value="119" name="_selected_action" data-version="2.0"/></td>
        <td>EFGH Engineering College</td>
        <td>2.0</td>
    </tr>
    <tr class="row1">
        <td><input type="checkbox" class="action-select" value="119" name="_selected_action" data-version="1.0"/></td>
        <td>IJKL Engineering College</td>
        <td>1.0</td>
    </tr>
    </tbody>
</body>
</html>

and use the following jquery implementation on the document.ready to do it.

$(document).ready(function(){
    $('input:checkbox[data-version="1.0"]').attr('disabled','disabled');
});

This is a cleaner implementation than looping through the objects

You can refer to using custom data attributes from here for example

http://api.jquery.com/data/

An example of the same is available at the below URL:

http://jsfiddle.net/LUFJw/1/

And if you would like to make this functionality more generic, you can as well try this.

$(document).ready(function() {
    function disableVersion(version) {
        $('input:checkbox[data-version="' + version + '"]').attr('disabled', 'disabled');
    }
    disableVersion('1.0');
});

An example of the same is here : http://jsfiddle.net/LUFJw/2/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM