Javascript 禁用按钮点击但启用另一个按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11112537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 04:30:46  来源:igfitidea点击:

Disable button onclick but enable another button

javascripthtml

提问by austinh

I have two different buttons on my page. I want them both to be enabled when the page loads, but when a user clicks one I would like to disable the other button. But if they click the other button, I would like that button to disabled as well while enabling the other disabled button. I have been able to disable the button onclick, but I'm having trouble getting the other button to re-enable . Here are the two buttons that I have on the page. They are not in a form, just on the page.

我的页面上有两个不同的按钮。我希望在页面加载时启用它们,但是当用户单击一个按钮时,我想禁用另一个按钮。但是,如果他们单击另一个按钮,我希望在启用另一个禁用按钮的同时禁用该按钮。我已经能够禁用 onclick 按钮,但是我无法让另一个按钮重新启用。这是我在页面上的两个按钮。它们不是形式,只是在页面上。

<button onclick="down7913.disabled=false" type="submit" class="positive" name="up7913"><img src="check.png" alt=""/></button>

<button onclick="this.disabled=true" type="submit" class="negative" name="down7913"><img src="cross.png" alt=""/></button>

回答by swapnesh

Check this code, it's in your flavour and working:

检查此代码,它符合您的口味并且可以正常工作:

Code Snippet -

代码片段 -

<button
    onclick="this.disabled=true;document.getElementById('down7913').disabled=false;"
    type="submit"
    class="positive"
    name="up7913"
    id="up7913"
  >
    First
  </button>

  <button
    onclick="this.disabled=true;document.getElementById('up7913').disabled=false;"
    type="submit"
    class="negative"
    name="down7913"
    id="down7913"
  >
    Second
  </button>

回答by Ajay Beniwal

You should write functions:

你应该写函数:

function disablefirstbutton() {
    document.getElementById("firstbutton").disabled = true;
    document.getElementById("secondbutton").disabled = false;
}

function disablesecondbutton() {
    document.getElementById("secondbutton").disabled = true;
    document.getElementById("firstbutton").disabled = false;
}

<button id="firstbutton" onclick="disablefirstbutton()">
<button id="secondbutton" onclick="disablesecondbutton()">

回答by asprin

If you don't mind using jQuery, here it is!

如果你不介意使用 jQuery,它就在这里!

$(function() {
    $('button').click(function() {
        var classname = $(this).attr('class');

        if(classname == 'positive')
        { 
            $('button.positive').attr('disabled', 'disabled');
            $('button.negative').attr('disabled', false);
        }
        else
        {
            $('button.negative').attr('disabled', 'disabled');
            $('button.positive').attr('disabled', false);
        }
    });
});

回答by fanfavorite

You could try something like:

你可以尝试这样的事情:

onclick="document.getElementsByClassName('negative')[0].disabled=false"

That is pure javascript and please note the document.getElementsByClassName('negative')returns an arrayof all classes. If there is only one, then it will work as I wrote. If not, then I would add an id and use document.getElementById('buttonId').disabled=false.

那是纯 javascript,请注意所有类的document.getElementsByClassName('negative')返回值array。如果只有一个,那么它会像我写的那样工作。如果没有,那么我会添加一个 id 并使用document.getElementById('buttonId').disabled=false.