限制html中的点击按钮 [英] limit click button in html

查看:24
本文介绍了限制html中的点击按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图限制一个只能点击 5 次的按钮来添加单选按钮.添加单选按钮 5 次后,它将被禁用.这件事的javascript代码是什么?单击后我只能禁用它一次.下面是代码

I am trying to limit a button that can be clicked only 5 times to add radio button. After 5 times adding radio button, it will be disabled. What is the javascript code for this matter? I only able to disable it once after it is clicked. Below is the code

html

<input type ='button' id="id" onClick="a();">
        <div id='a'></div>

javascript

function a(){
    document.getElementById('a').innerHTML += "<input type='radio'>";
    document.getElementById("id").disabled=true;
}

推荐答案

放置一个全局计数器并使用它

Place a global counter and play with it

var counter=0;

function a(){
if(counter<5){
    document.getElementById('a').innerHTML += "<input type='radio'>";
    counter++;
}
else{
    document.getElementById("id").disabled=true;
}
}

这篇关于限制html中的点击按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆