基于比较模式输入值和目标输入值更改按钮颜色

Change Button Color based Comparison Modal Input Value And Target Input Value

我尝试 google 寻找一种方法来改变以下情况的按钮颜色:

假设我有一个 onclick 按钮,我单击它会打开一个模式框,其中包含一个输入框和一个提交按钮。

  1. 如果我在输入框中输入“90”并提交它会与 我的目标值“100”

  2. 如果我输入的值小于目标值,onclick 按钮将 变为红色,其文本将变为“90”​​

  3. 如果我输入一个大于目标值的值,onclick按钮 将变为绿色,其文本将变为“90”​​

请帮忙!任何想法都会有所帮助。

// Get the button that opens the modal
var btn1 = document.getElementById('b1');
// When the user clicks on the button, open the modal 
btn1.onclick = function() {
    modal.style.display = "block";}
// Get the modal
var modal = document.getElementById('myModal');
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
    modal.style.display = "none";
}
// Arithematic Operator Control
var target_value = document.getElementById('inputtarget');
function checkValue(){ 
  var inputvalue = document.getElementById('modal1');
  var buttonsubmit = document.getElementById('b1');
  var value = parseInt(inputvalue.value);
  var targetValue = parseInt(target_value.value);
 
  if (value < targetValue){
    buttonsubmit.style.background = 'red' ;
    buttonsubmit.innerText = value ; 
  }
  else if (value > targetValue){
    buttonsubmit.style.background = 'green';
    buttonsubmit.innerText = value ; 
  }
  else{
    buttonsubmit.style.background = '';
    buttonsubmit.innerText = '1'; 
  }
  modal.style.display = "none";
  return false;
}
#inputtarget {
  height: 60px;
  width: 100px;
  font-size: 1.5rem;
  text-align: center;
  border: 1;
  }
/* The Modal (background) */
.modal {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1; /* Sit on top */
    left: 0;
    top: 0;
    width: 100%; /* Full width */
    height: 100%; /* Full height */
    background-color: rgb(0,0,0); /* Fallback color */
    background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}

/* Modal Content/Box */
.modal-content {
    background-color: #fefefe;
    margin: 15% auto; /* 15% from the top and centered */
    padding: 20px;
    border: 1px solid #888;
    width: 240px;
    height: 200px;
}
#modal1 {
  height:70px;
  width:100px;
  text-align: center;
}
/* The Close Button */
.close {
    color: #aaa;
    float: right;
    font-size: 28px;
    font-weight: bold;
}

.close:hover,
.close:focus {
    color: black;
    text-decoration: none;
    cursor: pointer;
  }
<!-- Button and Target Value -->
<div>
<button id="b1" onclick="return checkValue()">1</button>
<input id="inputtarget" type="number" ondrop="returnfalse;" onpaste="returnfalse;" 
           onkeypress='return event.charCode>=48 && event.charCode<=57';><br>
</div>  

<!-- The Modal Box -->
<div id="myModal" class="modal">

  <!-- Modal content -->
  <div class="modal-content">
    <span class="close">&times;</span>
    <p>PLEASE INPUT QUANTITY</p>
    <input id=modal1 type="number" ondrop="returnfalse;" onpaste="returnfalse;" 
           onkeypress='return event.charCode>=48 && event.charCode<=57'; 
           style=font-size:20px><br>
    <br>
    <button id="submit" onclick="return checkValue()">SUBMIT</button>
  </div>
</div>

这里是 jsfiddle

中的相同代码