的Javascript
(function() {
$(function() {
var productChooser;
productChooser = function() {
this.quantity = ko.observable(0);
this.productId = ko.observable(1);
this.genderId = ko.observable(1);
this.sizeId = ko.observable(1);
this.colorId = ko.observable(1);
this.setGenderAndSize = function(stringtoparse) {
var values;
console.log(stringtoparse);
values = stringtoparse.split(":");
alert("fasdfs");
this.genderId(values[0]);
this.sizeId(values[1]);
};
};
return ko.applyBindings(new productChooser(), $("#genderAndSizeChooser")[0]);
});
}).call(this);
HTML
<div class="btn-group" id="genderAndSizeChooser">
<button type="button" class="btn btn-size dropdown-toggle" data-toggle="dropdown">
Size <span class="caret"></span>
</button>
<ul class="dropdown-menu sizegen-text" role="menu">
<li class="active"><a href="">CHOOSE A SIZE...</a></li>
<li><a data-bind='click: setGenderAndSize.bind("2:2")'>MALE XS</a></li>
<li><a data-bind='click: setGenderAndSize.bind("2:2")'>MALE S</a></li>
<li><a data-bind='click: setGenderAndSize.bind("2:2")'>MALE M</a></li>
<li><a data-bind='click: setGenderAndSize.bind("2:2")'>MALE L</a></li>
<li><a data-bind='click: setGenderAndSize.bind("2:2")'>MALE XL</a></li>
<li><a data-bind='click: setGenderAndSize.bind("2:2")'>FEMALE XS</a></li>
<li><a data-bind='click: setGenderAndSize.bind("2:2")'>FEMALE S</a></li>
<li><a data-bind='click: setGenderAndSize.bind("2:2")'>FEMALE M</a></li>
<li><a data-bind='click: setGenderAndSize.bind("2:2")'>FEMALE L</a></li>
<li><a data-bind='click: setGenderAndSize.bind("2:2")'>FEMALE XL</a></li>
</ul>
</div>
我无法从setGenderAndSize
获得价值。一个简单的console.log(stringtoparse)
输出视图模型productChooser {quantity: function, productId: function, genderId: function, sizeId: function, colorId: function…}
如何获取setGenderAndSize
传递的值?
答案 0 :(得分:0)
我只是想通了你需要添加传递的值只是第二个参数:
<div class="btn-group" id="genderAndSizeChooser">
<button type="button" class="btn btn-size dropdown-toggle" data-toggle="dropdown">
Size <span class="caret"></span>
</button>
<ul class="dropdown-menu sizegen-text" role="menu">
<li class="active"><a href="">CHOOSE A SIZE...</a></li>
<li><a data-bind='click: setGenderAndSize.bind($data, "2:2")'>MALE XS</a></li>
<li><a data-bind='click: setGenderAndSize.bind($data, "2:2")'>MALE S</a></li>
<li><a data-bind='click: setGenderAndSize.bind($data, "2:2")'>MALE M</a></li>
<li><a data-bind='click: setGenderAndSize.bind($data, "2:2")'>MALE L</a></li>
<li><a data-bind='click: setGenderAndSize.bind($data, "2:2")'>MALE XL</a></li>
<li><a data-bind='click: setGenderAndSize.bind($data, "2:2")'>FEMALE XS</a></li>
<li><a data-bind='click: setGenderAndSize.bind($data, "2:2")'>FEMALE S</a></li>
<li><a data-bind='click: setGenderAndSize.bind($data, "2:2")'>FEMALE M</a></li>
<li><a data-bind='click: setGenderAndSize.bind($data, "2:2")'>FEMALE L</a></li>
<li><a data-bind='click: setGenderAndSize.bind($data, "2:2")'>FEMALE XL</a></li>
</ul>
</div>