javascript - 如何使用 jQuery/JS 将下拉项设置为选中状态?

标签 javascript jquery html forms thymeleaf

我希望有人可以帮助解决我遇到的问题。我正在开发一个表单,该表单正在我们正在开发的网络应用程序上使用。我创建了一个标题字段,当单击“其他”时,它将显示另一个下拉菜单(带有其他标题)。我的问题是一旦保存表单,我们返回编辑表单,如果选择了“其他” - 它不会显示所选的其他标题。

例如,您是先生 - 我们保存表单 - 返回表单编辑客户详细信息 - 它不会显示所选的标题,但如果我们再次保存表单 - 它仍然显示为先生 - 所以它正在选择它来自页面,只是不明显地显示它。

这是片段:

function titlehandler(objName)
{

var titlefield = objName.value;
document.patientDetailsForm.hiddentitle.value = titlefield;
extratitleupdate();
}

function extratitleupdate()
{

var titlevalue = document.patientDetailsForm.hiddentitle.value;
if (titlevalue == "Other")
{
    document.getElementById("othertitle").disabled = false;
    document.getElementById("othertitle").hidden = false;
    document.getElementById("other-label").style.display = 'none';
}
else
{
    document.getElementById("othertitle").disabled = true;
    document.getElementById("othertitle").hidden = true;
    document.getElementById("other-label").style.display = '';
}
}   
<form name="patientDetailsForm">

<fieldset  class="title-box">

<span><input type="radio" name="title" value="Mr" onclick="titlehandler(this)" id="titlemr" th:field="*{title}" /> <label for="titlemr" id="mr-label">Mr</label></span>
<span><input type="radio" name="title" value="Mrs" onclick="titlehandler(this)" id="titlemrs" th:field="*{title}" /> <label for="titlemrs" id="mrs-label">Mrs</label></span>
                        <span><input type="radio" name="title" value="Miss" onclick="titlehandler(this)" id="titlemiss" th:field="*{title}" /> <label for="titlemiss" id="miss-label">Miss</label></span>
<span><input type="radio" name="title" value="Ms" onclick="titlehandler(this)" id="titlems" th:field="*{title}" /> <label for="titlems" id="ms-label">Ms</label></span>
<span><input type="radio" name="title" value="Other" onclick="titlehandler(this)" id="titleother" th:field="*{title}" /><label for="titleother" id="other-label">Other</label>
<select name="name" class="multidrop" id="othertitle" disabled="" hidden="" th:field="*{title}"><option value="Dr">Dr</option>
<option value="Rev">Rev</option>
<option value="Sir">Sir</option>
<option value="Sist">Sist</option></select>
</span>
<input type="hidden" name="hiddentitle" value="" />
<input type="hidden" name="valid" value="true" />
<input type="hidden" th:field="*{personId}" />
</fieldset>
</form>

最佳答案

您可以使用document.getElementById("othertitle").selectedIndex = "0";来选择下拉列表的第一个元素。

如果你想使用 jQuery ,你可以这样做:

$("#othertitle").val("Sir");

这是工作示例:

function titlehandler(objName)
{

var titlefield = objName.value;
document.patientDetailsForm.hiddentitle.value = titlefield;
extratitleupdate();
}

function extratitleupdate()
{

var titlevalue = document.patientDetailsForm.hiddentitle.value;
if (titlevalue == "Other")
{
    document.getElementById("othertitle").disabled = false;
    document.getElementById("othertitle").hidden = false;
    document.getElementById("other-label").style.display = 'none';
}
else
{
    document.getElementById("othertitle").disabled = true;
    document.getElementById("othertitle").hidden = true;
    document.getElementById("other-label").style.display = '';
    document.getElementById("othertitle").selectedIndex = "0";

}
}
<form name="patientDetailsForm">

<fieldset  class="title-box">

<span><input type="radio" name="title" value="Mr" onclick="titlehandler(this)" id="titlemr" th:field="*{title}" /> <label for="titlemr" id="mr-label">Mr</label></span>
<span><input type="radio" name="title" value="Mrs" onclick="titlehandler(this)" id="titlemrs" th:field="*{title}" /> <label for="titlemrs" id="mrs-label">Mrs</label></span>
                        <span><input type="radio" name="title" value="Miss" onclick="titlehandler(this)" id="titlemiss" th:field="*{title}" /> <label for="titlemiss" id="miss-label">Miss</label></span>
<span><input type="radio" name="title" value="Ms" onclick="titlehandler(this)" id="titlems" th:field="*{title}" /> <label for="titlems" id="ms-label">Ms</label></span>
<span><input type="radio" name="title" value="Other" onclick="titlehandler(this)" id="titleother" th:field="*{title}" /><label for="titleother" id="other-label">Other</label>
<select name="name" class="multidrop" id="othertitle" disabled="" hidden="" th:field="*{title}"><option value="Dr">Dr</option>
<option value="Rev">Rev</option>
<option value="Sir">Sir</option>
<option value="Sist">Sist</option></select>
</span>
<input type="hidden" name="hiddentitle" value="" />
<input type="hidden" name="valid" value="true" />
<input type="hidden" th:field="*{personId}" />
</fieldset>
</form>

关于javascript - 如何使用 jQuery/JS 将下拉项设置为选中状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45478920/

相关文章:

javascript - 返回多个值并访问它们?

javascript - jquery 移动内容垂直居中

javascript - NodeJS fs.watch 对 Docker 容器内的更改没有反应

javascript - 使用 ExtJS 选择不包含特定 ID 的元素

jquery - 单击 jquery + air 打开文件系统窗口

html - 更好的是(从性能的 Angular 来看)用 z-index 堆叠隐藏元素或将它们移开 'horizontally' ?

javascript - 使用 iframe 避免 Web 应用程序的缓存问题

c# - 查看在 Asp.Net core 3 的 Controller 中找不到操作

javascript - 是否有获取 XML 节点的平面字符串表示形式的默认机制?

javascript - 在 html 表中循环并获取每个 tr 的每个 td 的值