Frage

I have a table in which there are checkboxes, some checkboxes are disabled. I want to add an onclick attribute to all disabled checkboxes using JavaScript or jQuery.

HTML

<table class="docs_table" id="myTable2">
<tr id="node-22" class="disabled_element">
<td title="Document can't be selected.">
    <input type="checkbox" name="docs" id="/root/docname" value="/root/docname" disabled />
</td>
</tr>
<tr id="node-23" class="">
<td title="">
    <input type="checkbox" name="docs" id="/root/docname2" value="/root/docname2" />
</td>
</tr>
<tr id="node-24" class="disabled_element">
<td title="Document can't be selected.">    
    <input type="checkbox" name="docs" id="/root/docname3" value="/root/docname3" disabled />
</td>
</tr>
</table>

In above code the docname input box should get below as onclick.

onclick="javascript:if(confirm('Press OK to confirm')){ document.location='/pathtoscript?command=dosomething&amp;name=docname'};return false;"

and docname3 should get the below

onclick="javascript:if(confirm('Press OK to confirm')){ document.location='/pathtoscript?command=dosomething&amp;name=docname3'};return false;"

What I tried

 $('table tr td input[type="checkbox"]').each(function () {
    if ($( this ).prop('disabled')) {
        $( this ).closest('tr').addClass('lineThrough');
        $( this ).attr('onClick','onclick="javascript:if(confirm('Press OK to confirm')){ document.location='/pathtoscript?command=dosomething&amp;name=docname'};return false;"');
    }
});

But it doesn't work, and also I wonder how to deal with different docname because in onclick statement name=docname will be different for each input box.

Fiddle

War es hilfreich?

Lösung 2

In case, it is not possible for you to change your existing HTML structure and want to perform the action on click of the closest <td> element, please use the below code.

Javascript (jQuery)

$(document).ready(function(){
$('table tr td input[type="checkbox"]').each(function () {
    if ($( this ).prop('disabled')) {
        $( this ).closest('tr').addClass('lineThrough');
        var valStr = $(this).val();
        $( this ).closest('td').attr('onClick', 'clickFn("'+valStr+'");');
        //console.log($( this ).closest('td').attr('onClick'));
    }});
});

function clickFn(valStr){ 
    if(confirm('Press OK to confirm')){ 
        valStr = valStr.substring(valStr.lastIndexOf('/') + 1);
        newHref = '/pathtoscript?command=dosomething&amp;name=' +valStr;
        //console.log(newHref);
        document.location = newHref;
    }
}

Demo

Andere Tipps

I did it using this answer.

<table class="docs_table" id="myTable2">
    <tr id="node-22" class="disabled_element">
        <td title="Document is being edited by someone.">
            <div style="display:inline-block; position:relative;">
                <input type="checkbox" name="docs" id="/root/docname" value="/root/docname" disabled />
                <div style="position:absolute; left:0; right:0; top:0; bottom:0;"></div>
            </div>
        </td>
    </tr>
    <tr id="node-23" class="child">
        <td title="">
            <input type="checkbox" name="docs" id="/root/docname2" value="/root/docname2" />
        </td>
    </tr>
    <tr id="node-22" class="disabled_element">
        <td title="Document is being edited by someone.">
            <div style="display:inline-block; position:relative;">
            <input type="checkbox" name="docs" id="/root/docname3" value="/root/docname3" disabled />
                <div style="position:absolute; left:0; right:0; top:0; bottom:0;"></div>
            </div>
        </td>

 $('table tr td input[type="checkbox"] + div').each(function () {
     if ($(this).prev('input[disabled]')) {
         $(this).closest('tr').addClass('lineThrough');
         $(this).click(function () {
             if (confirm('Press OK to confirm')) {
                 var docname = $(this).prev('input[disabled]').prop('value');
                 docname = docname.substring(docname.lastIndexOf('/') + 1);
                 document.location = '/pathtoscript?command=dosomething&amp;name=' + docname;
             }
         });
     }
 });

Disabled elements don't generate mouse events. So basically what I did is added a div around the disabled inputs and added some JS to match it.

JSFiddle

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top