【发布时间】:2015-08-10 11:03:21
【问题描述】:
我的 Aurelia 视图中有一个 select html 标记(使用 select2),我想在其中基于布尔值将 selected 属性添加到 option 标记表达。表达式只会为真一次。这是我的代码(这很好用):
<select class="js-example-basic-single" style="width:200px;">
<option repeat.for="region of personRegionDetail" value="${region.Id}">${region.Name}</option>
</select>
我正在我的 option 标签中寻找类似的东西(如果可能的话):
${if(region.Id === persondetail.RegionId){ selected }}
我也尝试过使用 jQuery,但这只会在我刷新页面时显示我选择的选项:
<script type="text/javascript">
//Initialize select2
$('select.js-example-basic-single').select2();
//I tried this
//The searched option value contains ${persondetail.RegionId}
$('select.js-example-basic-single').val("${persondetail.RegionId}").trigger("change");
</script>
我通过单击使用route-href="route:routename" 的链接从另一个视图访问该页面(如果它说明了什么)。
我的问题是如何解决这个问题以在我进入页面时显示所选选项?
编辑: 我需要在几毫秒后初始化 select2 以显示所选选项(可能我必须等待 API 响应)。这导致了 jQuery 的问题。
【问题讨论】:
标签: jquery html jquery-select2 aurelia