gpt4 book ai didi

javascript - 基于更改按钮颜色的比较模态输入值和目标输入值

转载 作者:行者123 更新时间:2023-11-28 03:49:58 26 4
gpt4 key购买 nike

我尝试通过谷歌搜索找到一种方法来更改以下情况的按钮颜色:

假设我有一个 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 中的相同代码

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com