【发布时间】:2022-01-06 22:20:37
【问题描述】:
我已将此复选框添加到我的表单中:
<div class="row">
<div class="form-group col">
<input type="checkbox" name="prd_presell" id="product_presell"
onclick="presellTXT()" value="TRUE" @if(!empty($product)) @if($product->prd_presell == 'TRUE') checked @endif @endif>
<label for="presell_product">Presell Product</label>
</div>
</div>
所以有一个名为 presellTXT() 的 onClick 函数如下所示:
function presellTXT() {
var presell_checkbox = document.getElementById("product_presell"); // get checkbox
var presell_text = document.getElementById("show_presell_text"); // text div
var presell_text_input = document.getElementById("presell_product_text"); // text input
if (presell_checkbox.checked == true){
presell_text.style.display = "block";
} else {
presell_text_input.value = "";
presell_checkbox.value = "";
presell_text.style.display = "none";
}
}
所以当复选框被选中时,它基本上显示了id为show_presell_text的元素:
<div class="row" id="show_presell_text">
<div class="col-lg-12">
<div class="form-group">
<label>Product Presell Text:</label>
<input name="prd_presell_text" id="presell_product_text" class="form-control" value="{{ old('prd_presell_text', !empty($product) ? $product->prd_presell_text : '') }}">
</div>
</div>
</div>
因此,当页面加载时,它不应显示产品预售文本,除非选中该复选框。
现在,如果您查看jsfillde 示例,您可以看到页面加载后,文本输入出现,但复选框根本没有选中!
那么如何在页面加载且未选中复选框的情况下隐藏文本输入?
注意:我不想为此使用 CSS,因为我需要正确确定是否在页面加载时检查了 prd_presell(因为 @if($product->prd_presell == 'TRUE') 从数据库中检索数据)那么它必须显示 Product Presell Text 文本输入。
更新:
我尝试添加这个:
#show-presell-text.hidden {
display: none !important;
}
并将其添加到 div:
<div class="row" id="show_presell_text" class="@if(!empty($product)) @if($product->prd_presell != 'TRUE') hidden @endif @endif">
但不起作用并隐藏元素。
【问题讨论】:
-
document.getElementById("product_presell").value ,勾选不勾选总是true
标签: javascript html laravel forms checkbox