[英]Assign value to Knockout observable?
I have a page that populates its fields(dropdowns and textboxes) from the DB when the page loads via VIEWDATA object from controller. 当页面通过控制器的VIEWDATA对象加载时,我有一个页面从数据库填充其字段(下拉列表和文本框)。
I am using KnockoutJS for data-binding, below is the scenario 我正在使用KnockoutJS进行数据绑定,下面是场景
HTML & JS(dropdown) HTML&JS(下拉列表)
@if (Filter!= null)
{
<select data-bind="options: RsnAdmAct,
value: selectedRsnAdmAct,
optionsCaption:'Choose...',
optionsValue:'RsnAdminActionID',
optionsText:'RsnAdminActionDescp',
optionsAfterRender: function()
{
setOptionRSN(@Filter.RsnForAdminAction);
}">
</select>
}
self.RsnAdmAct = ko.observableArray([]);
self.selectedRsnAdmAct = ko.observable();
$.getJSON("GetRAA", null, function (data) {
self.RsnAdmAct(data);
});
self.selectedRsnAdmAct = ko.observable();
self.setOptionRSN = function (x) {//reason for admin action dd
self.selectedRsnAdmAct(x);
};
this does update the drop-down with the assigned value " X ". 这确实会使用分配的值“ X ”更新下拉列表。
But the same does not assign value for the text-boxes as below 但是,相同的做法并没有为文本框分配值,如下所示
HTML & JS(textbox) HTML和JS(文本框)
@if (@Filter != null)
{
<input placeholder="Downstream"
data-bind="value: DownStream,
optionsAfterRender:function()
{
setOptionDstream(@Filter.DownstreamNumber);
}">
}
self.DownStream = ko.observable();
self.setOptionDstream = function (x) {//down stream number
self.DownStream(x);
};
optionsAfterRender
applies to options; optionsAfterRender
适用于选项; a text input has no options. 文本输入没有选项。 Setting the value of
DownStream
will set the value of the text box, because that's how the value
binding works. 设置
DownStream
的值将设置文本框的值,因为这是value
绑定的工作方式。
I tried to search for a proper way to do this but below is the way it works and no other alternative worked.I had to move the function which assigns the value to the texbox observable into the options after render attribute of the drop down as below 我试图寻找一种合适的方法来做到这一点,但是下面是它的工作方式,没有其他替代方法了。
@if (Filter!= null)
{
<select data-bind="options: RsnAdmAct,
value: selectedRsnAdmAct,
optionsCaption:'Choose...',
optionsValue:'RsnAdminActionID',
optionsText:'RsnAdminActionDescp',
optionsAfterRender: function()
{
setOptionRSN(@Filter.RsnForAdminAction);
setOptionDstream(@Filter.DownstreamNumber);
}">
</select>
}
This is the only way it worked for me.Thank you for the posts and comments 这是它对我有用的唯一方法。谢谢您的帖子和评论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.