繁体   English   中英

如何在JavaScript中启用禁用SUP标签

[英]How to enable disable sup tag in javascript

我有2个单选按钮和一个文本框,当单击“是”单选按钮时,我需要使(*)出现在文本框中;如果没有单击任何单选按钮,则将其删除。
我的观点:

  <label>code<sup id="star" disabled>*</sup></label>             
      >Add to electronic<input type="radio" name="yes" id="yes"/>onchange="starenable()"/><input type="radio" name="yes"
        > id="no"/>onchange="starenable()"/>no

我的JavaScript:

 function starenable() {
        if (document.getElementById('yes').checked) {
            document.getElementById("star").disabled = false;
        } else if (document.getElementById('no').checked) {
            document.getElementById("star").disabled = true;
        }
    }

我尝试了这一点,但观点没有改变。 有任何想法吗?

输入具有禁用属性。用输入更改sub 。然后以正确的格式对齐html代码。然后添加*取决于选中的单选按钮

更新

注意: sub标签没有禁用属性

 function starenable() { if (document.getElementById('yes').checked) { document.getElementById("star").disabled = false; } else if (document.getElementById('no').checked) { document.getElementById("star").disabled = true; } document.getElementById("star").innerHTML = document.getElementById('yes').checked ? '*' : ''; } 
 <label>code <sub id="star"></sub></label> Add to electronic <input type="radio" name="yes" id="yes" onchange="starenable()" />yes <input type="radio" name="yes" id="no" onchange="starenable()"/>no 

这是您的解决方案https://jsfiddle.net/me9uwL2d/

<input type="radio" name="test" value="Yes" />Yes
<input type="radio" name="test" value="No" />No
<input type="text" />

$('input[type="radio"]').change(function() {
    if( $(this).is(':checked') && ($(this).attr('value') == 'Yes')) {
        $('input[type="text"]').val("*");
    }else{
        $('input[type="text"]').val('');
    }
});

 $(document).ready(function() { $('input[type=radio][name=yes]').change(function() { if (this.id == 'yes') { $('#star').hide(); } else if (this.id == 'no') { $('#star').show(); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <label>code<span id="star" >*</span></label> Add to electronic <input type="radio" name="yes" id="yes"/>yes <input type="radio" name="yes" id="no"/>no 
用这个

<label>code<sup id="star" style="visibility:hidden">*</sup></label>
Add to electronic
<input type="radio" name="yes" id="yes" onchange="starenable()"/>yes
<input type="radio" name="yes" id="no" onchange="starenable()"/>no

function starenable() {

    if (document.getElementById('yes').checked)

        document.getElementById("star").style.visibility = "visible";

    else if (document.getElementById('no').checked) 

        document.getElementById("star").style.visibility = "hidden";

    }

暂无
暂无

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

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