简体   繁体   English

如果下拉列表等于 1,则禁用耦合输入区域

[英]Disabling couple input area if dropdown equal to 1

I am stuck up with this on my php page.我在我的 php 页面上遇到了这个问题。 I can't disable 3 input area after selected dropdown选择下拉菜单后我无法禁用 3 个输入区域

I Just want to disable irrelevant input areas if type of slider selected like 1 otherwise do nothing如果 slider 的类型选择为 1,我只想禁用不相关的输入区域,否则什么也不做

HTML Code which will use for condition: HTML 代码将用于条件:

 <div class="form-group"> <label for="slider_type">Slider Type</label> <select name="slider_type" class="form-select" id="slider_type" required> <option value="" disabled selected>Please Select</option> <option value="1">Image</option> <option value="2">Video</option> </select> </div>

HTML Code Which i want to disable if slider_type equal to 1 HTML 代码 如果 slider_type 等于 1,我想禁用它

 <label for="slider_title">Slider Title</label> <input type="text" name="slider_title" id="slider_title" class="form-control round" placeholder="Slider Title" onchange="DisableSliderInputArea()" required> </div> </div> <div class="col-md-6 mb-4"> <div class="form-group"> <label for="slider_description">Slider Body</label> <input type="text" name="slider_description" id="slider_description" class="form-control round" placeholder="Slider Body" required> </div> </div> <div class="col-md-6 mb-4"> <div class="form-group"> <label for="slider_button_link">Slider Button Link</label> <input type="text" name="slider_button_link" id="slider_button_link" class="form-control round" placeholder="Slider Button Link" required> </div> </div>

I tried this JavaScript code lines for 1 input area but it's not worked我为 1 个输入区域尝试了这个 JavaScript 代码行,但它不起作用

 <script type="text/javascript"> function DisableSliderInputArea(){ if(document.getElementById("slider_type").value=="1"){ document.getElementById("slider_title").disabled = true; } else { document.getElementById("slider_title").disabled = false; } } </script>

What's really wrong?真的有什么问题?

You almost have done all the job, one thing that was missing is the actual call of the function DisableSliderInputArea once your select box has changed its' value.您几乎已完成所有工作,缺少的一件事是 select 框更改其值后,实际调用 function DisableSliderInputArea You needed to add an event listener, so once user changes the selected option, your function will get triggered, and the textarea will be disabled or enabled.您需要添加一个事件侦听器,因此一旦用户更改所选选项,您的 function 将被触发,并且 textarea 将被禁用或启用。

Feel free to run the snippet below, and see how it works.随意运行下面的代码片段,看看它是如何工作的。 I added comments on the lines you need to add in JS section.我在 JS 部分中添加了您需要添加的行的注释。

 function DisableSliderInputArea() { if (document.getElementById("slider_type").value == "1") { document.getElementById("slider_title").disabled = true; } else { document.getElementById("slider_title").disabled = false; } } // Get the select out of the DOM and store in a local variable const dropdown = document.getElementById("slider_type"); // Attach an event listener, so once the select changes // its' value, this function will be called dropdown.addEventListener("change", DisableSliderInputArea);
 <div class="form-group"> <label for="slider_type">Slider Type</label> <select name="slider_type" class="form-select" id="slider_type" required> <option value="" disabled selected>Please Select</option> <option value="1">Image</option> <option value="2">Video</option> </select> </div> <label for="slider_title">Slider Title</label> <input type="text" name="slider_title" id="slider_title" class="form-control round" placeholder="Slider Title" onchange="DisableSliderInputArea()" required> </div> </div> <div class="col-md-6 mb-4"> <div class="form-group"> <label for="slider_description">Slider Body</label> <input type="text" name="slider_description" id="slider_description" class="form-control round" placeholder="Slider Body" required> </div> </div> <div class="col-md-6 mb-4"> <div class="form-group"> <label for="slider_button_link">Slider Button Link</label> <input type="text" name="slider_button_link" id="slider_button_link" class="form-control round" placeholder="Slider Button Link" required> </div> </div>

you're almost done, just incorrectly putting onchange="DisableSliderInputArea()"你快完成了,只是错误地把 onchange="DisableSliderInputArea()"

 function DisableSliderInputArea(){ if(document.getElementById("slider_type").value=="1"){ document.getElementById("slider_title").disabled = true; } else { document.getElementById("slider_title").disabled = false; } }
 <div class="form-group"> <label for="slider_type">Slider Type</label> <select name="slider_type" class="form-select" id="slider_type" onchange="DisableSliderInputArea()" required> <option value="" disabled selected>Please Select</option> <option value="1">Image</option> <option value="2">Video</option> </select> </div> <label for="slider_title">Slider Title</label> <input type="text" name="slider_title" id="slider_title" class="form-control round" placeholder="Slider Title" required> <div class="col-md-6 mb-4"> <div class="form-group"> <label for="slider_description">Slider Body</label> <input type="text" name="slider_description" id="slider_description" class="form-control round" placeholder="Slider Body" required> </div> </div> <div class="col-md-6 mb-4"> <div class="form-group"> <label for="slider_button_link">Slider Button Link</label> <input type="text" name="slider_button_link" id="slider_button_link" class="form-control round" placeholder="Slider Button Link" required> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM