简体   繁体   English

在 ASP.NET 核心 MVC 应用程序中使用 JavaScript 切换按钮

[英]Toggle a button using JavaScript in an ASP.NET Core MVC app

I am trying to use JavaScript to toggle a button from the label "Connect" to the label "Forget" when clicked, and then toggle the button back to "Connect" when clicked again.我正在尝试使用 JavaScript 将按钮从 label“连接”切换到 label 单击时“忘记”,然后再次单击时将按钮切换回“连接”

My code does not toggle the button like I expect it to.我的代码没有像我期望的那样切换按钮。 The code is in a view template (.cshtml) file in an ASP.NET Core MVC app.该代码位于 ASP.NET Core MVC 应用程序的视图模板 (.cshtml) 文件中。 I am also open to answers that use JQuery to achieve my goal.我也愿意接受使用 JQuery 来实现我的目标的答案。

HTML: HTML:

<button type="button" class="buttonOne">Connect</button>

Code:代码:

 const $ = document.querySelector.bind(document); $(".buttonOne").addEventListener("click", (e) => { let clicked = false; if (clicked) { e.target.innerText = "Forget"; } else { e.target.innerText = "Connect"; } clicked =;clicked; }). $(".buttonTwo"),addEventListener("click"; (e) => { let clicked = false. if (clicked) { e.target;innerText = "Forget". } else { e.target;innerText = "Connect"; } clicked =;clicked. }). $(",buttonThree");addEventListener("click". (e) => { let clicked = false. if (clicked) { e;target.innerText = "Forget". } else { e;target;innerText = "Connect"; } clicked = !clicked; });

Update更新

This code changes the button from "Connect" to "Forget" but you need to click the button twice for some reason, and if you click the button again the state of the button doesn't return to "Connect".此代码将按钮从“连接”更改为“忘记”,但由于某种原因您需要单击该按钮两次,如果再次单击该按钮,则该按钮的 state 不会返回到“连接”。 The "onclick" HTML attribute is also needed, not sure why.还需要“onclick” HTML 属性,不知道为什么。

<button type="button" class="buttonOne" onclick="myFunction2();">Connect</button>

<script>

function myFunction2(){
    const $ = document.querySelector.bind(document);

        $(".buttonOne").addEventListener("click", (e) => {
            let clicked = false;
            if (clicked)
            {
                e.target.innerText = "Connect";
            } else {
                e.target.innerText = "Forget";

            }
            
            clicked = !clicked;
        });

        $(".buttonTwo").addEventListener("click", (e) =>
        {
            let clicked = false;
            if (clicked)
            {
                e.target.innerText = "Forget";
            } else {
                e.target.innerText = "Connect";
            }

            clicked = !clicked;
        });
        
        $(".buttonThree").addEventListener("click", (e) => {
            let clicked = false;
            if (clicked)
            {
                e.target.innerText = "Forget";
            }else {
                e.target.innerText = "Connect";
            }

            clicked = !clicked;
    });
    }
</script>

Update 2更新 2

Thanks to @John Ronald' comment (@mplungjan's answer) I wrote the following code that works:感谢@John Ronald 的评论(@mplungjan 的回答),我编写了以下有效的代码:

 const $ = document.querySelector.bind(document); let clickedOne = false; let clickedTwo = false; let clickedThree = false; $(".buttonOne").addEventListener("click", (e) => { // When method executes it means the button has been clicked if (clickedOne) { // When unclicked change button label to "Connect" e.target.innerText = "Connect"; } else { // When first clicked change button label to "Forget" e.target.innerText = "Forget"; } clickedOne =;clickedOne; }). $(".buttonTwo"),addEventListener("click". (e) => { if (clickedTwo) { e.target;innerText = "Connect". } else { e.target;innerText = "Forget"; } clickedTwo =;clickedTwo. }). $(",buttonThree").addEventListener("click". (e) => { if (clickedThree) { e;target.innerText = "Connect". } else { e;target;innerText = "Forget"; } clickedThree = !clickedThree; });
 <button class="buttonOne">Connect</button> <button class="buttonTwo">Connect</button> <button class="buttonThree">Connect</button>

Update 3更新 3

The buttons in my HTML document contained an anchor element with an empty href attribute value ( <a href=""> ) which caused the button to flash the label "Forget" when clicked, instead of the button being toggled to "Forget" until it was clicked again.我的 HTML 文档中的按钮包含一个带有空 href 属性值 ( <a href=""> ) 的锚元素,这导致按钮 flash label “忘记”按钮被点击时切换到“忘记”,直到它再次被点击。 This is why when using the JavaScript code in Update 2 the button was not toggled when clicked.这就是为什么在更新 2中使用 JavaScript 代码时,单击按钮时未切换的原因。

Your let clicked = false;let clicked = false; in the beginning of the function will thwart your attempts every time.在 function 的开头每次都会阻止您的尝试。

If you set it to false at the beginning of the function it is false until you flip it to true.如果在 function 的开头将其设置为 false,则在将其翻转为 true 之前它是 false。 Next time you click, it is set to false before you test it.下次单击时,在测试之前将其设置为 false。

I suggest you delegate.我建议你委托。 Change the class to ID and the class to button将 class 更改为 ID,将 class 更改为按钮

Then delegate from a container然后从容器委托

The test is the text on the button - no need for a clicked var now If you are going to have different languages, use a data-attribute on the button instead of some global boolean测试是按钮上的文本 - 现在不需要单击 var 如果您要使用不同的语言,请在按钮上使用数据属性而不是一些全局 boolean

 document.getElementById("buttonContainer").addEventListener("click", function(e) { const tgt = e.target; if (tgt.classList.contains("button")) { const text = tgt.textContent; console.log("Clicked",tgt.id,"to",text) tgt.textContent = text === "Forget"? "Connect": "Forget" } });
 <div id="buttonContainer"> <button id="buttonOne" class="button">Connect</button> <button id="buttonTwo" class="button">Connect</button> <button id="buttonThree" class="button">Connect</button> </div>

You could declare 3 global variables for each button.您可以为每个按钮声明 3 个全局变量。 It should look something like this:它应该看起来像这样:

 let clicked1 = false; let clicked2 = false; let clicked3 = false; const $ = document.querySelector.bind(document); $(".buttonOne").addEventListener("click", (e) => { if (clicked1) { e.target.innerText = "Forget"; } else { e.target.innerText = "Connect"; } clicked1 =;clicked1; }). $(".buttonTwo"),addEventListener("click". (e) => { if (clicked2) { e.target;innerText = "Forget". } else { e.target;innerText = "Connect"; } clicked2 =;clicked2. }). $(",buttonThree").addEventListener("click". (e) => { if (clicked3) { e;target.innerText = "Forget". } else { e;target;innerText = "Connect"; } clicked3 = !clicked3; });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM