我有两个字段出现,因为之前已经输入了数据:
在某些情况下,if
条件被调用,这些值需要被清除。我试图在 val()
中传递空白字符串,使其变为空
但它不工作,数据仍然显示:
<div class="col-md-12 fromDates" style="margin-bottom: 10px; display: block;">
<label for="fromDate">From Date</label>
<input type="text" id="assessFrom" required="" name="assessFrom">
</div>
<div class="col-md-12 toDates" style="margin-bottom: 10px; display: block;">
<label for="fromDate">To Date</label>
<input type="text" id="assessTo" required="" name="assessTo">
</div>
if (assessmentType == "firm" || assessmentType == "product") {
$('.fromDates').val('');
$('.toDates').val('');
}
最佳答案
.fromDates
和 .toDates
是没有值的 div
元素。
相反,您需要清除其中的 input
元素的值:
if (assessmentType == "firm" || assessmentType == "product") {
$('.fromDates input, .toDates input').val('');
}
或者,您可以直接在输入中使用 id
:
if (assessmentType == "firm" || assessmentType == "product") {
$('#assessFrom, #assessTo').val('');
}
关于javascript - 如何完全清除输入字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53556807/