[英]how to bind multiple events in data-bind using knockout
Newbie here in knockout JS. 新手在这里淘汰JS。 Im still learning things.
我还在学习东西。 Im trying to bind multiple events in a single data-bind.
我试图在单个数据绑定中绑定多个事件。 How can i achieve this?
我怎样才能做到这一点? I need to execute 2 functions on the single event.
我需要在单个事件上执行2个函数。
HTML 的HTML
<span class="btn btn-default btn-file btn-primary">Browse<input type="file" id="ImportFile" data-bind="value:file,event: { change: $root.Browse }"></span>
FUNCTION #1 功能#1
function Browse(data) {
var data = new FormData();
var files = $("#ImportFile").get(0).files;
if (files.length > 0) {
$("#tab1").removeClass("active");
$("#tab2").addClass("active");
$("#tabSelect").removeClass("active");
$("#tabImport").addClass("active");
fileName(files[0].name);
data.append("UploadedFile", files[0]);
self.changeMethod = function (data, event) {
self.file(event.currentTarget.files[0]);
self.fileSize(event.currentTarget.files[0].size);
}
}
}
FUNCTION #2 功能#2
self.fileChange=function(data){
if (window.File && window.FileReader && window.FileList && window.Blob)
{
//get the file size and file type from file input field
var fsize = data.size;
var ftype = data.type;
if(fsize>10)
{
alert(fsize +" bites\nToo big!");
self.file("")
}
switch(ftype)
{
case 'image/png':
case 'image/gif':
break;
default:
alert('Unsupported File!');
self.file("")
}
}else{
alert("Please upgrade your browser, because your current browser lacks some new features we need!");
}
};
You can provide another function and call the 2 functions that you want to call from the new function. 您可以提供另一个功能,并从新功能中调用要调用的2个功能。
Here is a sample 这是一个样本
var viewModel = function() { var self = this; self.isFirstMethodCalled = ko.observable(false); self.isSecondMethodCalled = ko.observable(false); self.triggerFirstAndSecondMethods = function(data){ self.firstMethod(data); self.secondMethod(data); }; self.firstMethod = function(data){ self.isFirstMethodCalled(true); } self.secondMethod = function(data){ self.isSecondMethodCalled(true); } }; ko.applyBindings(new viewModel());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script> <input type="button" data-bind="click: triggerFirstAndSecondMethods" value="Call First And Second Method"/> <div data-bind="visible: isFirstMethodCalled"> First Method has been called. </div> <div data-bind="visible: isSecondMethodCalled"> SecondMethod has been called. </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.