首页 > 解决方案 > 如何获取所选值以在百里香中输入文本框

问题描述

下拉菜单中的 onchange,dist_nm 应该反映在文本框中。有人可以帮忙吗?

<select id="editDistName" th:field="*{districts}" th:onchange="selectedDistName();">
    <option value="default">Select the District </option>
    <option th:each="dist : ${districts}" th:value="${dist.dist_id}" 
            th:text="${dist.dist_nm}" th:selected="${dist.dist_nm}"/>
</select>
<input type="text" class="form-control" id="dist_nm" name="dist_nm"/>

标签: textboxthymeleaf

解决方案


1.)我不认为你的 th:selected="..."-attribute 有任何影响。

2.)试试这个js代码(使用jquery):

function selectedDistName()
{
   let txt = $("#editDistName option:selected").text();
   console.log(txt);
   $("#dist_nm").val(txt);
}

如果您不使用 jquery,请发表评论。


推荐阅读