[英]How to display error message when two fields are empty javascript/jquery?
How to display error messgae using javascript or jquery only when both the feilds are empty.仅当两个字段都为空时,如何使用 javascript 或 jquery 显示错误消息。 For example a user can either enter Zipcode or select a state from dropdown.例如,用户可以输入邮政编码或从下拉列表中选择一个状态。 If the user doesnot select any one of those then the error should be displayed.如果用户未选择其中任何一项,则应显示错误。
You can get the value of the elements by document.getElementById("id").value
可以通过document.getElementById("id").value
获取元素的document.getElementById("id").value
function Validate() { var zip = document.getElementById("zip").value; var state = document.getElementById("state").value; if(zip == "" && state == 0) { document.getElementById("error").innerHTML = "Error message"; } else { document.getElementById("error").innerHTML = ""; } }
<input type="text" id="zip"> <select id="state"> <option value="0">Select a state</option> <option value="State 1">State 1</option> <option value="State 2">State 2</option> <option value="State 3">State 3</option> <option value="State 4">State 4</option> </select> <p id="error"></p> <button onclick="Validate()">Submit</button>
Note : You can disable an element when the other is selected using document.getElementById("id").disabled = true;
注意:当使用document.getElementById("id").disabled = true;
选择另一个元素时,您可以禁用一个元素document.getElementById("id").disabled = true;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.