jquery 取值 数值_在jQuery中获取数据绑定值

I am using knockout js to set a span value.

HTML Code

this is working fine and showing user name on the UI.

I am trying to get that value from js file.

by using below code

alert($(this).attr('data-bind'));

this is serving result like this text: $data.OnLineUserName . I want the username assigned by me.

In UI its showing Bhagirathi but in js its showing the content present in the data-bind

how to get the Name(means: Bhagirathi) in js file

please help to solve this problem

thanks in advance

[EDIT]

$(document).on("click", ".btn-mini", function (e) {

alert(ko.contextFor($('.btn-mini')[0]).$data.OnLineUserName);

try {

var connectionId = chatHub.server.getUserConnectionId($(this).attr('data-bind').username, sessionUserName);

}

catch (e) {

//error

}

});

[/EDIT]

解决方案

You can get the knockout context for element with

ko.contextFor($('#spnQStreamChat').get(0))

this will return an an object like

ko.bindingContext {$parents: Array[1], $root: ViewModel, ko: Object, $data: SomeObject, $parentContext: ko.bindingContext…}

where $data is your $data object. So to get the name you need something like

ko.contextFor($('.button.btn.c_btn').get(0)).$data.OnLineUserName()

This way is more useful when you need to get $data object. Otherwise you can just get the 'text' of a span with jQuery

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值