简体   繁体   English

选择特定选项时禁用Javascript按钮

[英]Javascript Disable button when a specific option is selected

I have a code here but the code for disabling the button is not working. 我这里有一个代码,但是禁用按钮的代码不起作用。

Button with link 带链接的按钮

   <button id="link-button">
    <a id="link-web" href="" style="text-decoration:none">GO TO LINK</a>
   </button>

Select Option 选择选项

<select id="website" onchange='selecWeb(this)'>
  <option>Search Engine</option>
  <option value="https://google.com">Google</option>
  <option value="https://yahoo.com">Yahoo</option>
  <option value="https://bing.com">Bing</option>
 </select>

JS Code JS代码

function selecWeb(a){
    var val = a.value;

    if(a.options[a.selectedIndex].text== "Search Engine")
    {
        $('#link-button').prop('disabled', true);
    }
    else
    {
        $('#link-button').prop('disabled', false);
        document.getElementById('link-web').href = val;
    }
}

Why prop ('disabled', true) is not working? 为什么prop ('disabled', true)不起作用? Is there another way? 还有另一种方法吗?

使用JS 禁用属性:

document.getElementById("id").disabled = true;

The problem is that you have nested an a tag inside a button tag, which is not allowed by HTML5. 问题是您在button标记内嵌套了a标记,HTML5不允许这样做。 A button can not contain interactive content. 按钮不能包含交互式内容。 JSFiddle demonstrating this: http://jsfiddle.net/ykv695en/ JSFiddle对此进行了演示: http : //jsfiddle.net/ykv695en/

 function selecWeb(a){ var val = a.value; if(val == "SearchEngine") { $('#link-button').prop('disabled', true); } else { $('#link-button').prop('disabled', false); $('#link-button').off(); $('#link-button').click(function(){ location.href = val; }); } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="link-button"> GO TO LINK </button> <select id="website" onchange='selecWeb(this)'> <option value="SearchEngine">Search Engine</option> <option value="https://google.com">Google</option> <option value="https://yahoo.com">Yahoo</option> <option value="https://bing.com">Bing</option> </select> 

Using change add a disabled class that will disable the href value of the a tag based on the selects new value. 使用change添加禁用类,将禁用href的价值a基础上,选择新的价值标签。

 $('#website').change(function(event) { if($(this).val() === 'Search Engine') { $('#link-button a').addClass('is-disabled'); } else { $('#link-button a').removeClass('is-disabled'); $('#link-button a').attr('href', $(this).val()); } }); 
 a.is-disabled { color: currentColor; cursor: not-allowed; opacity: 0.5; text-decoration: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="link-button"> <a id="link-web" class="is-disabled" href="" style="text-decoration:none">GO TO LINK</a> </button> <select id="website"> <option>Search Engine</option> <option value="https://google.com">Google</option> <option value="https://yahoo.com">Yahoo</option> <option value="https://bing.com">Bing</option> </select> 

here are two things. 这是两件事。 first that : anchor tag is inside a button element second: anchor tag doesn't have disabled property. 首先:锚标记位于按钮元素内;其次:锚标记没有禁用属性。

so your code be something like this 所以你的代码是这样的

 <button id="link-button">GO TO LINK</button>
 <select id="website" onchange='selecWeb(this)'>
            <option>Search Engine</option>
            <option value="https://google.com">Google</option>
            <option value="https://yahoo.com">Yahoo</option>
            <option value="https://bing.com">Bing</option>
  </select>
<script>
function selecWeb(a) {
        var val = a.value;

        if (a.options[a.selectedIndex].text == "Search Engine") {
            $('#link-button').prop('disabled', true);
        }
        else {
            $('#link-button').prop('disabled', false);
            document.getElementById('link-web').href = val;
        }
    }
 </script>

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

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