切换单选按钮值?

时间:2020-11-04 12:47:49

标签: jquery

我想将.r-text的默认值设置为0,并且取消选中该项目时,.r-text的值将恢复为默认值0。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>//jQuery Plugin

<form>
    <label>
        <input type="radio" name="a1" value="30" />
    </label></br>
    <label>
        <input type="radio" name="a1" value="50" />
    </label></br>
    <label>
        <input type="radio" name="a1" value="100" />
    </label></br>
</form>
<span class="r-text"><?php echo $selected;?></span>
<script>

function deselectableRadios(rootElement) {
  if(!rootElement) rootElement = document;
  if(!window.radioChecked) window.radioChecked = null;
  window.radioClick = function(e) {
    const obj = e.target;
    if(e.keyCode) return obj.checked = e.keyCode!=32;
    obj.checked = window.radioChecked != obj;
    window.radioChecked = obj.checked ? obj : null;
  }
}

var deselect = deselectableRadios();

    $('input[type=radio]').click(function(e) {//jQuery works on clicking radio box
        var value = $(this).val(); //Get the clicked checkbox value
        $('.r-text').html("$"+value);

    if(deselect == true){
        $('.r-text').html("$"+ 0);
    }

    });

</script>

大家好,我想将.r-text默认值设置为0,并且取消选中该项目时,.r-text将恢复为默认值0。

0 个答案:

没有答案