猿问

如何在onclick之后禁用按钮,但在另一个功能中将其禁用?

我的按钮具有相同的onclick功能。我希望一键后禁用按钮。onclick函数需要执行多个函数,因此我该如何实现。


 <input type="button" class="buttons" id="button2" value="2" 

    onclick="run()" />  

下面是其中一个按钮的代码,run函数内的另一个函数正在运行。


function disablebutton(button){

    this.disabled = true;

}


森栏
浏览 512回答 3
3回答

MMMHUHU

您只需要在不同的按钮上调用该函数即可。&nbsp; &nbsp; &nbsp; &nbsp; function disablebutton(buttonObject){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buttonObject.disabled = true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; var button1 = document.getElementById('b1');&nbsp; &nbsp; &nbsp; &nbsp; var button2 = document.getElementById('b2');&nbsp; &nbsp; &nbsp; &nbsp; button1.onclick = function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; disablebutton(button1);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;button2.onclick = function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; disablebutton(button2);&nbsp; &nbsp; &nbsp; &nbsp; }或者&nbsp; &nbsp; <input type="button" class="buttons" id="button1" value="1"&nbsp;&nbsp; &nbsp; onclick="run(this)" />&nbsp;&nbsp;&nbsp; &nbsp; <input type="button" class="buttons" id="button2" value="2"&nbsp;&nbsp; &nbsp; onclick="run(this)" />&nbsp;&nbsp; &nbsp; function run(button){&nbsp; &nbsp; &nbsp; &nbsp; button.disabled = true;&nbsp; &nbsp; }

收到一只叮咚

您可以disablebutton()在run()函数内部调用或在函数之后将其添加run()到onClick():<input type="button" class="buttons" id="button2" value="2"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; onclick="run(); disablebutton(this)" />// Or<input type="button" class="buttons" id="button2" value="2"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; onclick="run(this)" />function run(thisButton) {&nbsp; console.log('run');&nbsp; disableButton(thisButton)}第一种解决方案:function run() {&nbsp; console.log('run');}function disablebutton(button){&nbsp; &nbsp; console.log(button);&nbsp; &nbsp; button.disabled = true;}<input type="button" class="buttons" id="button2" value="2"&nbsp;&nbsp; &nbsp; onclick="run(); disablebutton(this)" />第二个解决方案:function run(thisButton) {&nbsp; console.log('run');&nbsp; disablebutton(thisButton)}function disablebutton(button){&nbsp; &nbsp; console.log(button);&nbsp; &nbsp; button.disabled = true;}<input type="button" class="buttons" id="button2" value="2"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; onclick="run(this)" />

慕姐4208626

您必须选择所有按钮,然后更改Disabled属性。您可以通过在按钮上添加一个类,从该类中选择它们,然后循环遍历,将它们全部禁用来做到这一点,如下所示:var elems = document.getElementsByClassName("buttons");for(var i = 0; i < elems.length; i++) {&nbsp; &nbsp; elems[i].disabled = true;}
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答