我试图在谷歌周围寻找一种方法来更改以下情况的按钮颜色:

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


如果我在“输入”框中输入“ 90”并提交,它将与
我的目标值“ 100”
如果我输入的值小于目标值,则onclick按钮将
变成红色,其文本将更改为“ 90”
如果我输入的值大于目标值,则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中的相同代码

关于javascript - 更改基于按钮颜色的比较模态输入值和目标输入值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48088393/

10-12 12:55
查看更多