javascript - Passing value to a function with knockout - Stack Overflow

I use knockout here on a very basic example where I would like to pass the value of the clicked item to

I use knockout here on a very basic example where I would like to pass the value of the clicked item to the function. I tried something which doesn't work. Does someone can show me how to proceed? Maybe I'm doint the wrong way?

Thanks for your help.

<div class='liveExample'>   
    <h2 data-bind="value: 'A', click: myFunction">Aaaaa</h2>  
    <h2 data-bind="value: 'B', click: myFunction">Bbbbb</h2>
    <h2 data-bind="value: 'C', click: myFunction">Ccccc</h2>
</div>

// Here's my data model
var ViewModel = function() {

    this.myFunction = function (elm)
    {
        alert('you clicked: ' + elm);
    }
};

ko.applyBindings(new ViewModel()); // This makes Knockout get to work

jsFiddle here: /

PS: I know we can do ...click: function () { myFunction('A'); }"> but I think there is a better way.

I use knockout here on a very basic example where I would like to pass the value of the clicked item to the function. I tried something which doesn't work. Does someone can show me how to proceed? Maybe I'm doint the wrong way?

Thanks for your help.

<div class='liveExample'>   
    <h2 data-bind="value: 'A', click: myFunction">Aaaaa</h2>  
    <h2 data-bind="value: 'B', click: myFunction">Bbbbb</h2>
    <h2 data-bind="value: 'C', click: myFunction">Ccccc</h2>
</div>

// Here's my data model
var ViewModel = function() {

    this.myFunction = function (elm)
    {
        alert('you clicked: ' + elm);
    }
};

ko.applyBindings(new ViewModel()); // This makes Knockout get to work

jsFiddle here: http://jsfiddle/LkqTU/10229/

PS: I know we can do ...click: function () { myFunction('A'); }"> but I think there is a better way.

Share Improve this question edited Jul 7, 2013 at 9:45 Sergey Berezovskiy 236k43 gold badges439 silver badges466 bronze badges asked Jul 7, 2013 at 9:23 BronzatoBronzato 9,39230 gold badges125 silver badges227 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 1

You can get value from event target (which is h2 element):

// Here's my data model
var ViewModel = function() {

    this.myFunction = function (data, event)
    {
        debugger;
        alert('you clicked: ' + event.target.value);
    }
};

ko.applyBindings(new ViewModel());

Read more on click binding

Try:

this.myFunction = function (vm, event)
    {
        alert('you clicked: ' + event.srcElement);
    }
this.myFunction = function (val1, val2)
{
    ...;
}

and in binding you must to set:

<h2 data-bind="value: 'C', click: myFunction.bind($data, 'A', 'B')">Ccccc</h2>

This must help you. You can pass any count of values by this method.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745478068a4629437.html

相关推荐

  • javascript - Passing value to a function with knockout - Stack Overflow

    I use knockout here on a very basic example where I would like to pass the value of the clicked item to

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信