在jQuery中获取数据绑定值 [英] Get data-bind value in jquery

查看:333
本文介绍了在jQuery中获取数据绑定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用敲除js来设置跨度值.

I am using knockout js to set a span value.

HTML代码

<span id="spnQStreamChat" data-bind="text: $data.OnLineUserName"></span>

这很好,并且在用户界面上显示了用户名.

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

我正在尝试从js文件中获取该值. 通过使用以下代码

I am trying to get that value from js file. by using below code

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

此投放结果如下文本:$ data.OnLineUserName .我想要我分配的用户名.

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

在UI中,它显示Bhagirathi,但在js中,它显示数据绑定中存在的内容

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

如何在js文件中获取名称(意味着:Bhagirathi)

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

请帮助解决此问题

预先感谢

$(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]

推荐答案

您可以使用

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

这将返回一个类似

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

其中$ data是您的$ data对象.因此,要获得名称,您需要类似

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()

当您需要获取$ data对象时,这种方式会更有用.否则,您可以使用jQuery获得跨度的文本"

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

这篇关于在jQuery中获取数据绑定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆