简体   繁体   中英

Last Clicked Radio button ID

I have table which consists of multiple rows and each row consists of Radio button in the first column as follows:

<table class="table table-condensed span8" id="AllocationTable">
    <thead>
        <tr>
            <th title="Entity to Allocate" style="width: 30%">Entity</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model.EntityAllocations)
        {
            <tr>
                <td>
                    <input type="radio" name="radio" id="@item.ID" class="EntityRadioSelect" value="@item.ID" onclick="EntitySelect(this)" disabled="disabled" >
                </td>
            </tr>
        }
    </tbody>
</table>

Now I want to capture the Ids of the last clicked radio button I have tried something like this

function EntitySelect(select) {
    if ($(select).parent().data("lastClicked")){
        alert($(select).parent().data("lastClicked"));
    }
    $(select).parent().data("lastClicked", select.id);
}

But I am getting the wrong value as it is giving me current value and sometime other values which is not accepted answer.

var clicked='';
var preClicked='';
$("#AllocationTable").on("click",".EntityRadioSelect",function(){
    preClicked=clicked;
    clicked=$(this).attr("id");
});

remove onclick="EntitySelect(this)"

try this:

function EntitySelect(select) {
    var parent=$(select).parents('#AllocationTable');
    if (parent.data("lastClicked")){
        alert(parent.data("lastClicked"));
    }
    parent.data("lastClicked", select.id); 
}

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