如何使按钮可点击一次?

How to make a button clickable once?

提问人:ToMadToCode 提问时间:11/7/2023 最后编辑:JLRisheToMadToCode 更新时间:11/7/2023 访问量:82

问:

在我的代码中,我希望一个按钮可以单击一次。我在我下面放了 2 个按钮,因为我想禁用另一个按钮,以便在单击其中一个按钮时使用。

<div style = "position:absolute; left:625px; top:100px; background-color:white;">
    <button type="button" onclick="alert('The guy walks away disapointed, I think I made the wrong decision')" >Detain</button>
    <button type="button" onclick="alert('The guy sighs in relief and looked happy. The guards guide him to the exit. I think I made the right decision')">Free them</button>
         </div>
JavaScript HTML 按钮 单击

评论

0赞 gre_gor 11/7/2023
这回答了你的问题吗?Vanilla Javascript:如何使按钮只能点击一次

答:

0赞 Bob Martin 11/7/2023 #1

也许你使用我推荐你这段代码。this.disabled='true';

      style="
        position: absolute;
        left: 625px;
        top: 100px;
        background-color: white;
      "
    >
      <button
        type="button"
        id="btn1"
        onclick="alert('The guy walks away disapointed, I think I made the wrong decision') ; document.getElementById('btn2').disabled='true'; this.disabled='true'"
      >
        Detain
      </button>
      <button
        type="button"
        id="btn2"
        onclick="alert('The guy sighs in relief and looked happy. The guards guide him to the exit. I think I made the right decision'); document.getElementById('btn1').disabled='true'; this.disabled='true'"
      >
        Free them
      </button>
    </div>
1赞 ZeNix 11/7/2023 #2

让我检查一下我是否正确理解,您希望在按下相反的按钮时禁用一个按钮。

首先,我将你的代码分成两部分,使其更具可读性,你可以通过在标签中设置 JS 部分来合并它:<script></script>

<script>
   //JS code here
</script>

let detainButton = document.getElementById('detainButton');
let freeButton = document.getElementById('freeButton');

function handleDetain() {
  alert('The guy walks away disappointed. I think I made the wrong decision');
  detainButton.disabled = true;
  freeButton.disabled = false;
}

function handleFree() {
  alert('The guy sighs in relief and looks happy. The guards guide him to the exit. I think I made the right decision');
  freeButton.disabled = true;
  detainButton.disabled = false;
}
<div style="position: absolute; left: 625px; top: 100px; background-color: white;">
    <button type="button" onclick="handleDetain()" id="detainButton">Detain</button>
    <button type="button" onclick="handleFree()" id="freeButton">Free them</button>
</div>

现在,您将拥有 2 个单独的功能,其中包含警报并根据相反的按钮切换按钮的状态,而不是将警报设置到按钮中。disabled

祝您编码愉快!

0赞 Jasith Heshala 11/7/2023 #3

样式部分(CSS):

<style>
    div{
        position:absolute;
        left:625px;
        top:100px;
        background-color:white;
    }
</style>

HTML 部分:

<div>
        <button type="button" onclick="detainButton()" id="detain-button">Detain</button>
        <button type="button" onclick="freeThemButton()" id="free-them-button">Free them</button>
</div>

脚本部分(JavaScript):

<script>
    
    function detainButton(){
        alert('The guy walks away disapointed, I think I made the wrong decision');
        document.getElementById("free-them-button").disabled = true;
    }
    function freeThemButton(){
        alert('The guy sighs in relief and looked happy. The guards guide him to the exit. I think I made the right decision');
        document.getElementById("detain-button").disabled = true;
    }

</script>
0赞 Omar Samara 11/7/2023 #4

为每个按钮指定一个唯一的 ID,然后在单击时禁用另一个按钮,如以下示例所示

    <div style = "position:absolute; left:625px; top:100px; background-  color:white;">
<button id="btn1" type="button" onclick="alert('The guy walks away disapointed, I think I made the wrong decision'); document.getElementById('btn2').disabled = true;" >Detain</button>
<button id="btn2" type="button" onclick="alert('The guy sighs in relief and looked happy. The guards guide him to the exit. I think I made the right decision'); document.getElementById('btn1').disabled = true;">Free them</button>
     </div> 

评论

0赞 ToMadToCode 11/8/2023
非常感谢你,伙计。这就是我一直在找的!