- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 Knockout JS 的新手,我正在努力解决这个问题,需要你的指导。一切正常,我可以通过 Ajax
获取 ProductID
并且它是 ProductOffers
,但是当我第二次 dropdown
时却没有自行填充。
<table>
<thead>
<tr>
<th></th>
<th>Product</th>
<th>Product Offers</th>
<th>Price</th>
<th>Stock</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<select data-bind="options: Products, optionsText: 'Name',optionsValue: 'ID', value: ProductID, optionsCaption: '-'" />
</td>
<td data-bind="if: ProductID">
<select data-bind="options: ProductOffers, optionsText: 'Name',optionsValue: 'ID', value: ProductOfferID, optionsCaption: '-'" />
</td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<script type="text/javascript">
function Product(id, name) {
this.ID = id;
this.Name = name;
}
function Offer(id, name) {
this.ID = id;
this.Name = name;
}
var viewModel = {
Products: ko.observableArray(<%= LoadProducts() %>),
ProductID: ko.observable('0'),
ProductOfferID: ko.observable('0'),
ProductOffers: ko.observable("")
};
viewModel.ProductID.subscribe(function (newValue) {
if (typeof newValue != "undefined") {
//alert("Selected product is : " + newValue);
viewModel.ProductOffers = GetProductOffers(newValue);
//alert(viewModel.ProductOffers);
}
});
ko.extenders.numeric = function (target, precision) {
//create a writeable computed observable to intercept writes to our observable
var result = ko.computed({
read: target, //always return the original observables value
write: function (newValue) {
var current = target(),
roundingMultiplier = Math.pow(10, precision),
newValueAsNum = isNaN(newValue) ? 0 : parseFloat(+newValue),
valueToWrite = Math.round(newValueAsNum * roundingMultiplier) / roundingMultiplier;
//only write if it changed
if (valueToWrite !== current) {
target(valueToWrite);
} else {
//if the rounded value is the same, but a different value was written, force a notification for the current field
if (newValue !== current) {
target.notifySubscribers(valueToWrite);
}
}
}
});
//initialize with current value to make sure it is rounded appropriately
result(target());
//return the new computed observable
return result;
};
ko.applyBindings(viewModel);
function GetProductOffers(ProductID) {
alert("Fetching offers for Product : " + ProductID)
var Val = "";
jQuery.ajax({
type: "POST",
url: "testing.aspx/GetProductOffers",
data: "{ProductID: '" + ProductID + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function (msg) {
Val = msg.d;
},
error: function (jqXHR, exception) {
if (jqXHR.status === 0) {
alert('Not connect.\n Verify Network.' + jqXHR.responseText);
} else if (jqXHR.status == 404) {
alert('Requested page not found. [404]' + jqXHR.responseText);
} else if (jqXHR.status == 500) {
alert('Internal Server Error [500].' + jqXHR.responseText);
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed.' + jqXHR.responseText);
} else if (exception === 'timeout') {
alert('Time out error.' + jqXHR.responseText);
} else if (exception === 'abort') {
alert('Ajax request aborted.' + jqXHR.responseText);
} else {
alert('Uncaught Error.\n' + jqXHR.responseText);
}
}
});
return Val;
}
</script>
**编辑:** 这是蒂姆输入后发生的情况。
JSFiddle:http://jsfiddle.net/neodescorpio/sPrVq/1/
编辑:
这是 Web 方法,我已将其更改为根据 JSLint 生成有效的 JSON
。现在第二个下拉列表已填满,但问题是每当我更改产品时它的值都不会改变,会获取正确的值但下拉列表不会显示它们。
[WebMethod]
public static string GetProductOffers(long ProductID)
{
StringBuilder sbScript = new StringBuilder();
string json = "[{\"ID\": 0,\"Name\": \"Sorry ! No data found\"}]";
bool first = true;
List<DMS.ProductOfferDO> offers = ProductOffers.Get(ProductID);
if (offers != null && offers.Count > 0)
{
sbScript.Append("[");
foreach (var x in offers.OrderBy(d => d.IsCashOffer))
{
if (first)
{
sbScript.Append(string.Format("{{\"ID\": {0},\"Name\": \"{1}\"}}", x.ID, x.Name));
first = false;
}
else
{
sbScript.Append(string.Format(",{{\"ID\": {0},\"Name\": \"{1}\"}}", x.ID, x.Name));
}
}
sbScript.Append("]");
json = sbScript.ToString();
}
return json;
}
最佳答案
为什么将 ProductOffers
声明为 ko.observable("")
?它应该被声明为可观察数组:ProductOffers: ko.observableArray([]);
另外,在你的 JFiddle 中:
function GetProductOffers(ProductID) {
var Val = "[new Offer(1,'Name'),new Offer(2,'Product A'),new Offer(4,'Product B'),new Offer(5,'Product C')]";
return Val;
}
应该是:
function GetProductOffers(ProductID) {
var Val = [new Offer(1,'Name'),new Offer(2,'Product A'),new Offer(4,'Product B'),new Offer(5,'Product C')];
return Val;
}
编辑:
尝试按如下方式修改您的设置:
[WebMethod]
public static string GetProductOffers(long ProductID)
{
List<DMS.ProductOfferDO> offers = ProductOffers.Get(ProductID);
return JsonConvert.SerializeObject(offers);
}
当然,您需要导入:using Newtonsoft.Json;
。
你为什么使用post?它应该是一个获取:
function GetProductOffers(ProductID) {
$.get("testing.aspx/GetProductOffers",
{ ProductID: ko.toJSON(ProductID) }
)
.done(function (data) {
viewModel.ProductOffers(JSON.parse(data));
})
.fail(function (data) { })
.always(function () { });
}
编辑2:
viewModel.ProductID.subscribe(function (newValue) {
viewModel.ProductOffers.removeAll();
if (newValue) {
var productOffers = GetProductOffers(newValue);
viewModel.ProductOffers(productOffers);
}
});
请告诉我们事情进展如何!
关于javascript - knockout 在下拉菜单更改时填充字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23585353/
我想认真学习更多关于 ES6 的知识。我一直在网上做一些例子,虽然我得到了大部分例子,但有时我什至不知道从哪里开始。请注意,我对这个 ES6 和 Knockout 的东西 super 菜鸟,想通过从他
我正在使用 knockout.mapping 将我的 javascript 对象转换为可观察到的 knockout 。在这个 javascript 对象内部是一个嵌套对象,并且这个嵌套对象正在被转换,
我正在用 Kendo、Knockout 和 kendo-knockoutjs 库做一些实验。我想将 knockout View 模型与剑道数据源一起使用,并将其绑定(bind)到剑道网格小部件。 在剑
有谁知道现在当 jQuery.tmpl is not being actively developed or maintained anymore 时 Knockout 的默认模板引擎会发生什么? K
我面临一个问题,即添加新项目时计算的可观察数组未更新。 self.FilteredCityList = ko.computed(function() { var filteredCollectio
我想对数组中的多个属性使用 this fiddle 中的 ko.utils.arrayGetDistinctValues,因此我将数组映射到我想要的两个属性的数组 viewModel.justCate
我在使用 Knockout 的映射插件时遇到了一个奇怪的问题。 如果我通过映射填充一个 observable 数组,我将无法迭代该数组或获取其长度,即使 UI 已正确更新,该数组似乎也是空的。 你可以
所以我试图让 knockout 与 summernote 打得很好。它并没有真正起作用。我意识到这是因为 Summernote 使用了 而不仅仅是一个输入字段。 我的绑定(bind)是这样的: ko.
另一个我似乎无法找到帮助的 knockout 问题。 我基本上是在尝试实现级联下拉列表。前几天我请求帮助了解如何解析我的复杂 JSON(它来自 cakePHP Controller 。前几天我收到的帮
我在尝试使用映射插件将项目更新为 observableArray 的一部分时遇到问题。 我有以下代码 accounts.list = ko.mapping.fromJS(@(Html.Raw(Mode
有几件事我不明白。 根据 KO 文档(以及关于 SO 的许多帖子),在查询和编写 observable 时应该使用括号。但是在绑定(bind)子属性时,在我看来,是否使用括号并不重要。 = 都返回
我从 WCF 接收数据并将其绑定(bind)到表。我在这个论坛上得到了帮助,添加了一些计算字段,一切都很完美。我想在此表的页脚添加总计。我的页面的简单版本可以在 http://jsfiddle.net
我有一个使用 Knockout 显示过滤列表的页面。 View 模型有一个对象数组,以及一个使用 ko.utils.arrayFilter 来过滤数组的 ko.computed。 有时过滤会变得非常复
我正在使用 jquery quicksearch 来搜索由 knockout foreach 循环填充的表。 需要启动快速搜索元素 foreach 结束后 . 我尝试了几种方法,但到目前为止都没有成功
我正在尝试使用类似于 silverlight 中的数据表单的数据网格来创建数据表单功能。 我有一个 div 来显示从网格中选择的项目: $('#readDate').attr('data-bind',
我有一个带有分页功能的 View 模型,它维护 pageIndex 属性。 Viewmodel 有一个包含页面的页面数组。每个页面都有自己的 pageNumber 属性。 我的 View 模型的分页代
名称+复选框的单向绑定(bind)工作正常,但它最初不适用于单选按钮employeeTypeA,尽管它在viewmodel中的值为true,html显示单选按钮未设置,为什么会这样?
我正在尝试解决ko映射的一个小问题。场景是我的ViewModel基本上是对象的集合。每个对象都是通过json调用创建的,方法是: var ViewModel = ko.observableArray(
使用 computed 的主要区别是什么?函数和 subscribe在knockout.js 中的功能? 最佳答案 计算出的 observable 通常用于返回计算出的值。作为计算评估的一部分访问的任
我有带有项目验证的ko.observableArrays。用户可以将项目标记为已删除。将其标记为已删除时,我需要在该项目上禁用验证。 您如何动态禁用验证? 示例:http://jsfiddle.net
我是一名优秀的程序员,十分优秀!