可以选择所有单选按钮。但是我想授予用户仅选择一个单选按钮的权限。

这是HTML代码。

$jsqla = mysql_query("select * from products where id='$product_id'") or die(mysql_error());
$jfeta = mysql_fetch_assoc($jsqla);

$formats = explode(";", $jfeta['formats']);

<div class="">
    <?php foreach($formats as $v){ ?>
        <label style="line-height: 1.25em;display: block;width: 100px;margin-right: 10px;float: left;">
            <div id="format-id_<?php echo $v?>" style="border: 1px solid;border-radius: 9px;text-align: center;padding-top: 10px;padding-bottom:10px;padding-left: 3px;padding-right: 3px;border-color: #cccccc;font-family: 'SSemibold'; font-size: 13px; color: #44b7da;">
                <input type="radio" value="<?php echo $v; ?>" name="abc" style="visibility:hidden;" id="<?php echo $v ?>" onClick="changeColour(this)"/>
                <span style="margin:-17px auto auto 0px;display:block;"><?php echo $v; ?></span>
            </div>
        </label>
    <?php } ?>
</div>


这是JavaScript代码。

<script type="text/javascript">
    function changeColour(raid) {
        var raid1 = raid.id;
        document.getElementById("format-id_"+raid1).style.backgroundColor = "#cccccc";
    }
</script>

最佳答案

$(document).ready(function(e) {
   $(':radio').bind('change',function(){
    var th = $(this), id = th.attr('id');
    //alert(th);
   if(th.is(':checked')){
     $(':radio[id="'  + id + '"]').not($(this)).attr('disabled',true);
   }
   else
   {
      $(':radio[id="'  + id + '"]').not($(this)).attr('disabled',false);
   }
  });
});


DEMO

关于javascript - 当数据来自数据库时,如何一次只选择一个单选按钮,并在javascript中取消选择其他单选按钮?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27035015/

10-12 12:22
查看更多