敲除设置视图模型字段值
Knockout set view-model field value
Model
有一个字段 TotalCount
。单击按钮时,如何更改其值?
<input id="Qty" type="text" style="width:25px;"
value="@Model.TotalCount" />
var pageKoModel;
var pageKoModelData = @Html.Raw(Json.Encode(Model));
$(document).ready(function () {
pageKoModel = new Object();
ko.mapping.fromJS(pageKoModelData, {}, pageKoModel);
ko.applyBindings(pageKoModel);
});
data-bind 不在代码中。因此,尝试用此代码替换输入字段
<input id="Qty" type="text" style="width:25px;" data-bind="
value:TotalCount" />
如果还是不行,试试
<input id="Qty" type="text" style="width:25px;" data-bind="
value:pageKoModel.TotalCount" />
这对我有用:
<input tabindex="1" type="text" data-bind="value: $root.TotalCount" maxlength="5"
onkeypress="return onlyNumberKey(event);" style="width:35px;" />
Model
有一个字段 TotalCount
。单击按钮时,如何更改其值?
<input id="Qty" type="text" style="width:25px;"
value="@Model.TotalCount" />
var pageKoModel;
var pageKoModelData = @Html.Raw(Json.Encode(Model));
$(document).ready(function () {
pageKoModel = new Object();
ko.mapping.fromJS(pageKoModelData, {}, pageKoModel);
ko.applyBindings(pageKoModel);
});
data-bind 不在代码中。因此,尝试用此代码替换输入字段
<input id="Qty" type="text" style="width:25px;" data-bind="
value:TotalCount" />
如果还是不行,试试
<input id="Qty" type="text" style="width:25px;" data-bind="
value:pageKoModel.TotalCount" />
这对我有用:
<input tabindex="1" type="text" data-bind="value: $root.TotalCount" maxlength="5"
onkeypress="return onlyNumberKey(event);" style="width:35px;" />