Knockout to get the Attribute Value onClick functi

2019-03-04 01:42发布

问题:

HTML View with attr Value 'Qref'.

This is the HTML Code for bindling

Currently i have hard coded the Qref Attribute vaue

<!--ko if:$parent.Type == 2 -->
<input type="checkbox" data-bind="attr:{id: $data.Id , Qref: '3177'} , click: $root.answerClick">&nbsp;&nbsp;&nbsp;<span data-bind="text: $data.Text , attr:{id: $data.Id}"></span>
<!--ko if:$data.InputType == "text" -->
<input type="text">
<!-- /ko -->
<!-- /ko -->

This is the event for CLick.I am able to access the ID But not able to access the Qref Value.I want to know how can i access the Qref Value.

    answerClick: function (data ,event) {
                    var click_id = event.target.id;
                    return true;
                },

回答1:

You can access attribute values of a DOM using the getAttribute function.

This will work for you:

answerClick: function(c, event){
        var element = event.target;
        var qref = element.getAttribute('Qref');
        var click_id = element.id;
        return true;
    }