我在一个应用程序上有两个字段(A和B),这两个字段不能同时包含数据。我希望当有人填充了A,然后开始填充B(或者反之亦然)时,会抛出一条错误消息,告诉他们两个字段都不能包含数据。
如果两个字段都不等于null,我尝试使用javascript在onchange上抛出一个错误,但没有成功。我知道这也是一种简单的方式来满足我的意图。
<script>
//set IDs of selectors to use
var HighSchoolID = "ctl00$mainContent$CreateAccountUserControl$CreateUserControl$ProspectForm$datatel_highschoolacademichistoryd4d7c0cb_3dfc_451f_b497_62f37d538e48datatel_highschoolid$criteriaSchoolName"
var UnlistedSchoolID = "ctl00$mainContent$CreateAccountUserControl$CreateUserControl$ProspectForm$datatel_highschoolacademichistoryd4d7c0cb_3dfc_451f_b497_62f37d538e48datatel_unlistedschoolinfo$datatel_unlistedschoolinfo"
//attach OnChange event listener to fields
var HighSchool = document.getElemntByID(HighSchoolID);
var UnlistedSchool = document.getElementByID(UnlistedSchoolID);
confirmHighSchool.addEventListener("click",CheckUnlisted);
function CheckUnlisted(){
if(HighSchool != null && UnlistedSchool != null){
alert(ERROR);
}
}
</script>我希望当有人开始(或完成,任一工作)填写第二个字段时弹出错误消息。
编辑*抱歉,stackoverflow上的新手,包含更多代码。这两个字段是作为我们的CRM的一部分提供的,需要填写其中的一个。
发布于 2019-05-31 02:56:39
有几个错误。getElementByID和getElemntByID都不是函数
正确的函数是getElementById
尽管如此,您需要检查inputs的值,而不是输入的引用。
正如您所说的,您是新接触javascript的,我也推荐您这个链接:w3schools examples about forms/inputs and validation
查看此小提琴:
var highSchool = document.getElementById("highSchool");
var unlistedSchool = document.getElementById("unlistedSchool");
var confirmHighSchool = document.getElementById('confirmHighSchool');
confirmHighSchool.addEventListener("click", checkUnlisted);
function checkUnlisted() {
if(highSchool.value && unlistedSchool.value){
alert("ERROR MESSAGE TO BE ADDED");
}
}<label for"highSchool" >HighSchool</label>
<input type="text" id="highSchool">
<br>
<label for"unlistedSchool" >UnlistedSchool</label>
<input type="text" id="unlistedSchool">
<br>
<button id="confirmHighSchool">Confirm Highschool</button>
https://stackoverflow.com/questions/56383875
复制相似问题