简体   繁体   English

向必需属性添加条件

[英]adding conditions to required attribute

I have a form with mainly all simple textboxes and I am using the required attribute for the fields that are required.我有一个主要包含所有简单文本框的表单,并且我正在为所需的字段使用 required 属性。 However, I have a textarea that only needs to be required if the previous radio button is selected as "Yes", otherwise I don't want the textarea to be required.但是,我有一个 textarea,只有在前一个单选按钮被选择为“是”时才需要它,否则我不希望需要 textarea。 Here is the code for those 2 fields:这是这两个字段的代码:

<input id="ElecWiFi" name="ElecWiFi" required type="radio" 
value="Yes" />
<textarea id="WiFiNeeds" cols="50" name="WiFiNeeds" rows="3" 
placeholder="Electricity or Wi-Fi Needs"></textarea>

How would I make the textarea required only if Yes was selected in ElecWiFi field?仅当在 ElecWiFi 字段中选择 Yes 时,我将如何使 textarea 成为必需? Thanks in advance for any help.在此先感谢您的帮助。

dr1054博士1054

像这样将 javascript 添加到 click 属性

<input id="ElecWiFi" name="ElecWiFi" required type="radio" value="Yes" onclick="document.getElementById('WiFiNeeds').setAttribute('required', 'required')" />

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

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