首页 > 解决方案 > 高亮文本字段的逻辑操作

问题描述

所以我在这里有一个场景,需要根据使用 javascript/html 的条件突出显示一个字段

假设如果 A 和 B 字段为空,则需要 C 或 D 字段来提交请求,反之亦然。

标签: javascripthtml

解决方案


我不太明白你需要什么,你能更具体一点吗?你有什么尝试,你能发布一些你的代码,让我们了解你真正需要什么吗?但据我所知,您希望字段 A 和 B 具有值 OR C 和 D(反之亦然??),但如果 A 和 B 没有任何值,则 C 或 D 必须继续,对吗?无论如何,那个 .js 是可以理解的,所以你可以按照你想要的方式配置它..

    <!DOCTYPE html>
<html>
<body>

<input id="A" type="text">
<input id="B" type="text">
<input id="C" type="text">
<input id="D" type="text">

<button type="button" onclick="myFunction()">Submit</button>

<p id="demo"></p>

<script>
function myFunction() {
  var Aa, Bb, Cc, Dd;


  Aa = document.getElementById('A').value
  Bb = document.getElementById('B').value
  Cc = document.getElementById('C').value
  Dd = document.getElementById('D').value

  //if A and B fields has values = OK
  if(Aa !== null && Aa !== '' && Bb !== null && Bb !== '') {
   text = "Input OK";
    }
  else if (Cc !== null && Cc !== '' || Dd !== null && Dd !== ''){
   text = "Values A and B are empy, but C  OR D has values";
  }
  else{
   text = "NOT OK: Insert values into A AND B or C OR D"
  }

  document.getElementById("demo").innerHTML = text;
}
</script>

</body>

推荐阅读