dongnao3990 2012-10-02 17:29
浏览 61

带有jquery的活动单选按钮

I used all of the helpful answers on this site to try to build a jquery script to select radio buttons, but for some reason they don't work. Am I doing something wrong? For the sake of simplicity I've started back from square one.

I've attached the format that the php-generated form produces below. I tried multiple browsers, but was unable to get any button to select anything. Is there some complexity I'm not taking into account? The page is a .php page, and the form is generated in php.

Sorry for the basic question -- any help would be greatly appreciated. Not sure if it's relevant, but I want these buttons to work on mobile as well as various browsers.

Form Code:

    <form class="form-horizontal" method="get" action="submit.php" name="form">
    <div class="control-group">
        <div class="row-fluid">
            <div class="span2"></div>
            <div class="span8">
                <label class = "control-label" for="input1"> foreign policy </label>

                <div class="controls">
                    <label class="checkbox inline"><input type="radio" name="group1" id="1_l" value="1_l"> Liberal</label>
                    <label class="checkbox inline"><input type="radio" name="group1" id="1_m" value="1_m"> Moderate</label>
                    <label class="checkbox inline"><input type="radio" name="group1" id="1_c" value="1_c"> Conservative</label>
                </div>
            </div>
        </div>

        <div class="row-fluid">
            <div class="span2"></div>
            <div class="span8">
                <br>
                <center><button type="submit" class="btn btn-danger btn-large">Generate</button></center>
            </div>
            <div class="span2"></div>
        </div>
</form>

Jquery Code:

$('button').click(
function() {
    $('#1_l').attr('checked', true);
}
);
  • 写回答

3条回答 默认 最新

  • doulianqi3870 2012-10-02 18:09
    关注

    Make sure to only bind the handler after the DOM has been loaded:

    $(function() {
      $('button').click(function() {
        $('#1_l').attr('checked', true);
      });
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办