简体   繁体   中英

How to disable a submit button in javascript after clicking on it?

I'm using a button tag to create the button. I want to disable the button when I click on it. But I'm not able to do it. I hope someone here can help me...

You need to change the .disabled property of the button.

var btn = document.querySelector("#submit-btn");
btn.disabled = true;

Live example:

 var btn = document.querySelector("#submit-btn"); btn.addEventListener('click', function() { submit(); }); function submit() { btn.disabled = true; }
 #submit-btn { width: 100%; height: 30px; border: solid black .5px; } #submit-btn:disabled { background-color: gray; }
 <input type="submit" id="submit-btn" value="Submit">

Resourced from this question .

HTML <input type="button" id="btn1" onClick="clicked()" value="text">

JS

function clicked(){
    var btn1 = document.getElementById("btn1");
    btn1.disabled = true;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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