很难形容。这是一个片段。
(function() {
var PhoneNumber = function() {
this.name = ko.observable();
this.phone = ko.observable();
};
$('[data-mask="phone"]').inputmask({
mask: '(999)999-9999'
});
var vm = {
newNumber: ko.observable(new PhoneNumber()),
numbers: ko.observableArray([]),
};
console.log('vm', vm.numbers());
vm.numbersJson = ko.computed(function() {
return ko.toJSON(vm.numbers);
});
vm.newNumberJson = ko.computed(function() {
return ko.toJSON(vm.newNumber);
});
ko.applyBindings(vm);
//events
function addNewNumber() {
vm.numbers.push(vm.newNumber());
vm.newNumber(new PhoneNumber());
}
function removeNumber() {
var item = ko.dataFor(this);
var remVal = _.reject(vm.Numbers(), function(el) {
return el.name === item.name;
});
vm.Numbers(remVal);
};
$(document).on('click', '#btnAdd', addNewNumber);
$(document).on('click', '.btnRemove', removeNumber);
})();
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet" />
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" />
<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>
<script src="https://rawgit.com/RobinHerbots/jquery.inputmask/3.x/dist/jquery.inputmask.bundle.js"></script>
<table class="table table-condensed table-hover table-striped table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Phone</th>
<th></th>
</tr>
</thead>
<tbody data-bind="foreach:numbers">
<tr>
<td><span data-bind="text:name"></span>
</td>
<td><span data-bind="text:phone"></span>
</td>
<td class="form-group">
<button class="btn btn-sm btnRemove btn-default" type="button"> <i class="fa fa-trash"></i>
</button>
</td>
</tr>
</tbody>
<tfoot data-bind="with:newNumber">
<tr>
<td>
<input type="text" class="input-sm form-control" data-bind="textInput:name" />
</td>
<td>
<input type="text" class="input-sm form-control" data-mask="phone" data-bind="textInput:phone" />
</td>
<td>
<button class="btn btn-sm btn-default" id="btnAdd" type="button"><i class="fa fa-plus"></i>
</button>
</td>
</tr>
</tfoot>
</table>
<h2>Values</h2>
<p>Numbers: <span data-bind="text:numbersJson"></span>
</p>
<p>New Number:
<span data-bind="text:newNumberJson"></span>
</p>
请注意,当 DOM 准备好时,有电话输入掩码。当我们在电话文本框中输入时,输入掩码是有效的。
但是在将第一条记录添加到数组中并清除文本框后,输入掩码会丢失。
问题:我们如何才能将输入掩码保留在文本框上?