绑定选择选项的模板数据

发布于 2024-11-03 16:45:11 字数 564 浏览 3 评论 0原文

假设我有一个带有以下 html 代码的 jquery 模板:

<div>
<span id="myfirstname"></span>
<select id="mygender" multiple="multiple">
  <option value="1">-select-</option>
  <option value="2">Male</option>
  <option value="3">Female</option>
  <option value="4">NotSpecified</option>
</select>
</div>

我的 json 如下所示: { "FirstName": "John", "Gender": "Male" };

我可以使用 ${FirstName 绑定 myfirstname但我如何绑定下拉值?我使用了 ${Gender} 但它只显示值,而不显示选择了“男性”选项的下拉列表。

let's say i have a jquery template with this html code:

<div>
<span id="myfirstname"></span>
<select id="mygender" multiple="multiple">
  <option value="1">-select-</option>
  <option value="2">Male</option>
  <option value="3">Female</option>
  <option value="4">NotSpecified</option>
</select>
</div>

my json looks like : { "FirstName": "John", "Gender": "Male" };

I can bind myfirstname using ${FirstName} but how do i bind the drop down value? I used ${Gender} but that displays just the value and NOT the drop down list with the "Male" option selected.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你怎么这么可爱啊 2024-11-10 16:45:11

一种选择是这样的:

<div>
    <span id="myfirstname">${FirstName}</span>
    <select id="mygender" multiple="multiple">
        <option value="1">-select-</option>
        <option value="2" {{if Gender == 'Male'}}selected="selected"{{/if}}>Male</option>
        <option value="3" {{if Gender == 'Female'}}selected="selected"{{/if}}>Female</option>
        <option value="4" {{if Gender == 'NotSpecified'}}selected="selected"{{/if}}>NotSpecified</option>
    </select>
</div>

如果这太冗长,那么您可以创建一个函数来进行匹配,例如:

function match(value, choice) {
    return value === choice ? "selected='selected'" : "";   
}

然后执行以下操作:

<div>
    <span id="myfirstname">${FirstName}</span>
    <select id="mygender" multiple="multiple">
        <option value="1">-select-</option>
        <option value="2" ${match(Gender, 'Male')}>Male</option>
        <option value="3" ${match(Gender, 'Female')}>Female</option>
        <option value="4" ${match(Gender, 'NotSpecified')}>NotSpecified</option>
    </select>
</div>

One option would be something like:

<div>
    <span id="myfirstname">${FirstName}</span>
    <select id="mygender" multiple="multiple">
        <option value="1">-select-</option>
        <option value="2" {{if Gender == 'Male'}}selected="selected"{{/if}}>Male</option>
        <option value="3" {{if Gender == 'Female'}}selected="selected"{{/if}}>Female</option>
        <option value="4" {{if Gender == 'NotSpecified'}}selected="selected"{{/if}}>NotSpecified</option>
    </select>
</div>

If that is too verbose, then you could create a function to do the match like:

function match(value, choice) {
    return value === choice ? "selected='selected'" : "";   
}

Then do:

<div>
    <span id="myfirstname">${FirstName}</span>
    <select id="mygender" multiple="multiple">
        <option value="1">-select-</option>
        <option value="2" ${match(Gender, 'Male')}>Male</option>
        <option value="3" ${match(Gender, 'Female')}>Female</option>
        <option value="4" ${match(Gender, 'NotSpecified')}>NotSpecified</option>
    </select>
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文