jquery 模板选择
我有一个带有 N 个单选按钮的 jquery 模板,我需要选择具有从外部 html 文件加载模板后传递的值的单选按钮(使用 jQuery.tmpl() 方法)。有没有一种好的方法可以在模板中选择单选按钮,而无需多个 {{if}}、{{else}} 语句、重复的代码和选择器?
I have a jquery template with N radio buttons and I need to select radio button with value that I pass after loading template from external html file(using jQuery.tmpl() method). Is there a good way of selection of radio button in template without multiple {{if}}, {{else}} statements, duplicate code and selectors?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的数据以什么格式从服务器返回?如果是 JSON 数组,你能做这样的事情吗?
html:
javascript:
http://jsfiddle.net/JbTwB/1/
In what format is your data coming back from the server? If it's a JSON array, could you do something like this?
html:
javascript:
http://jsfiddle.net/JbTwB/1/
如果您有所需无线电的值,您可以执行以下操作:
这里假设
jInstance
是新创建的模板副本,并且desiredValue
与您想要预先选择的单选按钮的值。如果您只知道部分值,则可以使用其他 jQuery 选择器来定位该输入。
如果您正在寻找更通用的“表单预填充”例程,则需要建立某种格式来存储预填充值集,以及从该格式到模板的映射。但这是可以做到的,而且也不一定那么难。
If you have the value of the desired radio, you could do something like:
This assumes that
jInstance
is the newly-created copy of the template, and thatdesiredValue
exactly matches the value of the radio button you'd like to be pre-selected.If you only know part of the value, there are other jQuery selectors you can use to target that input.
If you're looking for a more general "form pre-filling" routine, you'll need to establish some kind of format for storing the set of pre-fill values, and a mapping from that format to the template. But it can be done, and it's not necessarily that hard, either.