如何将可见绑定与单选按钮可观察参数一起使用?
当此页面呈现时,会选择正确的单选按钮,但是当选择“sendemail”或“sms”单选按钮值时,应该会出现附加输入,对吧?
$(function () {
var rbViewModel = {
qrType: ko.observable('plaintext')
};
ko.applyBindings(rbViewModel);
});
然后是我的单选按钮
<input type="radio" name="txtType" value="plaintext" data-bind="checked: qrType" />Plaintext
<input type="radio" name="txtType" value="sendemail" data-bind="checked: qrType" />Send E-mail
<input type="radio" name="txtType" value="sms" data-bind="checked: qrType" />SMS
和我的输入:
<div data-bind="visible: qrType == 'sendemail'"><input type="text" id="txtEmailSubject" placeholder="E-mail subject" /></div>
<div data-bind="visible: qrType == 'sendemail'"><input type="text" id="txtEmailBody" placeholder="E-mail body" /></div>
<div data-bind="visible: qrType == 'sms'"><input type="text" id="txtSmsMsg" placeholder="SMS body" /></div>
div
元素上的属性是否有问题?我认为函数可以在每个 KnockoutJS 文档 的可见绑定中使用,如下所示:
data-bind="visible: qrType=='sendemail'"
The correct radio button is selected when this page renders, but the additional inputs should appear when either "sendemail" or "sms" radio button values are selected, right?
$(function () {
var rbViewModel = {
qrType: ko.observable('plaintext')
};
ko.applyBindings(rbViewModel);
});
Then my radio buttons
<input type="radio" name="txtType" value="plaintext" data-bind="checked: qrType" />Plaintext
<input type="radio" name="txtType" value="sendemail" data-bind="checked: qrType" />Send E-mail
<input type="radio" name="txtType" value="sms" data-bind="checked: qrType" />SMS
And my inputs:
<div data-bind="visible: qrType == 'sendemail'"><input type="text" id="txtEmailSubject" placeholder="E-mail subject" /></div>
<div data-bind="visible: qrType == 'sendemail'"><input type="text" id="txtEmailBody" placeholder="E-mail body" /></div>
<div data-bind="visible: qrType == 'sms'"><input type="text" id="txtSmsMsg" placeholder="SMS body" /></div>
Is there something wrong with the attribute on the div
elements? I thought functions were able to be used in the visible bindings per KnockoutJS documentation, like this:
data-bind="visible: qrType=='sendemail'"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您在数据绑定属性的表达式中使用可观察量时,需要使用 () 引用它。
它们需要看起来像:
visible: qrType() === 'sendemail'
When you use an observable in an expression in a data-bind attribute, you need to reference it with ().
They would need to look like:
visible: qrType() === 'sendemail'