2017-06-29 135 views
1

功能是:当选中其中一个复选框时,必须禁用另一个复选框。它的工作,但更新WordPress页面,并尝试编辑复选框后,我有以下问题: enter image description here用于禁用复选框不起作用的功能

首先我检查了显示摘录,然后更新的页面,你可以看到,有2这是检查

复选框”

<div><input type="checkbox" id="dx_article_excerpt" onclick="disableContentCheckbox()"><label for="dx_article_excerpt"><?php _e('Display Excerpt', 'dxeasypb'); ?></label></div> 
<div><input type="checkbox" id="dx_article_content" onclick="disableExcerptCheckbox()"><label for="dx_article_content"><?php _e('Display Content', 'dxeasypb'); ?></label></div> 

而且我这里还有功能框:

function disableContentCheckbox(){ 
      if (document.getElementById('dx_article_excerpt').checked) { 
       document.getElementById("dx_article_content").disabled = true; 
      } else { 
       document.getElementById("dx_article_content").disabled = false; 
      } 
     } 
     function disableExcerptCheckbox(){ 
      if (document.getElementById('dx_article_content').checked) { 
       document.getElementById("dx_article_excerpt").disabled = true; 
      } else { 
       document.getElementById("dx_article_excerpt").disabled = false; 
      } 
     } 

为什么会发生这种情况?

+3

为什么不使用单选按钮呢? – hRdCoder

+0

你可以粘贴一个例子吗? –

+0

复选框既可以选中也可以禁用。 – evolutionxbox

回答

1

只需在禁用时取消选中其他复选框。

我注释掉了document.getElementById("dx_article_content").disabled = true;只是为了向您展示您在选中其中一张并点击另一张时的样子。在您的代码中取消注释。

<div><input type="checkbox" id="dx_article_excerpt" onclick="disableContentCheckbox()"><label for="dx_article_excerpt"><?php _e('Display Excerpt', 'dxeasypb'); ?></label></div> 
 
<div><input type="checkbox" id="dx_article_content" onclick="disableExcerptCheckbox()"><label for="dx_article_content"><?php _e('Display Content', 'dxeasypb'); ?></label></div> 
 

 
<script> 
 
    function disableContentCheckbox() { 
 
    if (document.getElementById('dx_article_excerpt').checked) { 
 
     document.getElementById('dx_article_content').checked = false; 
 
     //document.getElementById("dx_article_content").disabled = true; 
 
    } else { 
 
     document.getElementById("dx_article_content").disabled = false; 
 
    } 
 
    } 
 

 
    function disableExcerptCheckbox() { 
 
    if (document.getElementById('dx_article_content').checked) { 
 
     document.getElementById('dx_article_excerpt').checked = false; 
 
     //document.getElementById("dx_article_excerpt").disabled = true; 
 
    } else { 
 
     document.getElementById("dx_article_excerpt").disabled = false; 
 
    } 
 
    } 
 
</script>

+0

这里是耶稣女士们,先生们。上帝保佑你 –

+0

@RumenPanchev很乐意帮忙= D –

+0

@RumenPanchev我还是不明白为什么单选按钮没有被使用。 – evolutionxbox

0

那么,我看到它的方式,你有两个选择。

你可以在捕捉事件时自己改变c​​hecked属性。

第二种选择是使用单选按钮。

+0

可以显示我第一个选择怎么办? –

+1

https://stackoverflow.com/questions/8206565/check-uncheck-checkbox-with-javascript –