2 回答

TA贡献2080条经验 获得超4个赞
这有效:
function DoSomethingOnChange(selectBox) {
if ($(selectBox).find(":selected").text().trim().toUpperCase() == "OTHER") {
$('.other-input').show();
}
else {
$('.other-input').hide();
$('.other-input').val(""); //clear out the value if required
}
}
当通过内联事件处理程序触发时,例如:
<select onchange="DoSomethingOnChange(this)">
<option></option>
<option>One</option>
<option>Two</option>
<option>Other</option>
</select>
前提是您的选项之一有其他文字。
function DoSomethingOnChange(selectBox) {
if ($(selectBox).find(":selected").text().trim().toUpperCase() == "OTHER") {
$('.other-input').show();
} else {
$('.other-input').hide();
$('.other-input').val("");
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select onchange="DoSomethingOnChange(this)">
<option></option>
<option>One</option>
<option>Two</option>
<option>Other</option>
</select>
<input class="other-input" hidden type=text placeholder="Other input" />

TA贡献1876条经验 获得超7个赞
这样的事情可能会奏效
$("select").each(function(i,obj) {
if ($(obj).val() === "Other") {
$(obj).parent().append("<input type=\"text\" name=\"" + $(obj).prop("name") + "\" />");
$(obj).remove();
}
});
添加回答
举报