javascript 通过单击单选按钮禁用-启用超链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13174201/
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-10-26 17:59:20  来源:igfitidea点击:

disable-enable a hyperlink by clicking on radio-buttons

javascript

提问by puneet2289

i have a hyperlink code like :Click Here and i want to customize this link on the basis of my radio button click,which are like :YES NO on radio-button value:'yes',i want to enable the hyperlink. while on radio-button value:'no',i want to disable the hyperlink. So,to make the hyperlink disable after clicking radio-button 'NO', i have written the following in disable-link() method :function disable-link() { //document.getElementById('disable-link').disabled=true; document.getElementById('disable-link').href = '#'; } Now,what i have to write in enable-link() method so that the hyperlink gets re-enabled after clicking radio-button 'YES'

我有一个像超链接的代码:点击这里 ,我想自定义单选按钮的点击,这是一样的基础上,此链接:YES NO 在单选按钮值:“是的”,我要启用的超链接。在单选按钮值:'no'上,我想禁用超链接。因此,为了在单击单选按钮“否”后禁用超链接,我在 disable-link() 方法中编写了以下内容:function disable-link() { //document.getElementById('disable-link').disabled =真; document.getElementById('disable-link').href = '#'; 现在,我必须在 enable-link() 方法中写什么,以便在单击单选按钮“是”后重新启用超链接

function enable-link()
{
    //document.getElementById('disable-link').disabled=false;
        // SOME LINE OF CODE TO RE-ENABLE THE LINK
}

document.getElementById('disable-link').disabled=true*/*false;-: it makes the disable but user is able to click on the hyperlink though.So, i have used "document.getElementById('disable-link').href = '#';" -: it makes the link unclickable too Please suggest.

document.getElementById('disable-link').disabled=true*/* false; -:它使禁用但用户可以点击超链接。所以,我使用了“ document.getElementById('disable-link').href = '#';” -:它也使链接无法点击请建议。

回答by Ibrahim Oudah

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>StackoverFlow answer for question</title>
<script type="text/javascript">

    var link,color;

 function disable_link() { 

  document.getElementById('testlink').disabled=true;

  link = document.getElementById('testlink').href;

  document.getElementById('testlink').removeAttribute('href');
  //document.getElementById('testlink').style.color = "grey";

   } 


 function enable_link() { 

  document.getElementById('testlink').setAttribute("href",link);

   } 


</script>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
  <p>
    <label>
      <input type="radio" name="RadioGroup1" value="radio" id="RadioGroup1_0" onchange="disable_link();" />
      Radio</label>
    <br />
    <label>
      <input type="radio" name="RadioGroup1" value="radio" id="RadioGroup1_1" onchange="enable_link();" />
      Radio</label>
    <br />
  </p>
  <a id="testlink" href="http://www.yahoo.com"> test </a>
</form>
</body>
</html>