好的,伙计们。我正在尝试创建一个小费计算器,我遇到了一些问题。问题是当用户输入一个值时,程序会询问(禁用enter按钮)用户是否希望包含提示,因此用户选择是或否。如果选择了其中任何一个,前面的问题就消失了,文字出现“小费包括在内”或“小费不包括在内”。现在,如果用户想要编辑上面的值,按钮将再次被启用,但是如果它点击了一个,它将再次询问用户是否想要包含提示。所以我的问题是,我怎样才能使它使它,无论那个按钮被点击多少次,那个函数(提问提示)只工作一次?
这里有一些代码,HTML
<h3>What is the bill?</h3>
<input type="number" id="bill" autocomplete="off">
<button id="enterTheBill">Enter</button>
<div id="askTip">
<h3> Would you like to include the tip?</h3>
<button id="yesTip">Yes</button>
<button id="noTip">No</button>
</div>
<h3 id="includedTip">*Tip is included*</h3>
<h3 id="excludedTip">*Tip is NOT included*</h3>
JavaScript:
document.getElementById('enterTheBill').onclick = askForTip
function askForTip() {
document.getElementById('askTip').style.display = 'block'
document.getElementById('enterTheBill').disabled = true
}
document.querySelector('#bill').onchange = enableButton
function enableButton() {
document.getElementById('enterTheBill').disabled = false
}
您可以在askForTip函数之上定义一个全局变量(标志),并可以设置在第一次执行askForTip时切换该标志,然后在执行askForTip时,您可以根据该标志检查是停止还是执行代码。