如果 type="password" 的输入字段被浏览器的自动填充模式填充,则密码可观察值始终未定义。我只在用户名字段中输入第一个字符,然后从下拉列表中选择名称。密码字段由浏览器填写。
<script type="text/javascript">
$(document).ready(function () {
var viewModel = function () {
var self = this;
self.UserName = ko.observable();
self.Password = ko.observable();
self.Submit = function () {
var un = self.UserName();
var pw = self.Password(); // Password value is undefined!!!!
alert(pw);
};
};
ko.applyBindings(new viewModel());
});
</script>
<form id="LoginForm" data-bind="submit: Submit">
<fieldset>
<label for="UserName">Username</label>
<input id="UserName" data-bind="value: UserName" name="UserName" type="text" />
<label for="Password">Password</label>
<input id="Password" data-bind="value: Password" type="password" />
<button type="submit">Login</button>
</fieldset>
</form>