使用JQuery在单选按钮中设置“已检查”无效

时间:2019-01-05 21:48:36

标签: javascript jquery radio-button

我有这个init-state功能,可以将javascript中的radio按钮设置为checked

我使用php echo

来调用此javascript函数

我尝试将php放在各处,但javascript仍未设置单选按钮php

这是我的checkedphp

javascript

<div class="row text-center"> <script type="text/javascript"> function checkRadio(serviceid) { $('#radio' + serviceid).prop('checked', true); } </script> <?php echo "<script type='text/javascript'>checkRadio({$row['serviceid']});</script>"; ?> <div class="col-md-3 col-sm-6 col-6 ad-image"> <input type="radio" name="radios" id="radio1" value="1" class="input-hidden" /> <label for="radio1"> <img src="http://placehold.it/70" alt="I'm radio" /> </label> </div> <div class="col-md-3 col-sm-6 col-6 ad-image"> <input type="radio" name="radios" id="radio2" value="2" class="input-hidden" /> <label for="radio2"> <img src="http://placehold.it/70" alt="I'm radio" /> </label> </div> </div> ,位于$row['serviceid'](仅运行一次)中,并且携带值1(例如while loop

我尝试放置$row['serviceid'] = 1,它工作正常。

但是它没有设置alert(serviceid)来设置状态

有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

原始答案可在此处找到:How to set radio button status with JavaScript

如果您只想将单选按钮状态设置为选中,那么您确实不需要jQuery,则只需使用原始javascript:


    function checkRadio(serviceid) {
        radiobtn = document.getElementById("radio" + serviceid);
        radiobtn.checked = true;
    }