我需要在选中复选框

时间:2016-06-03 06:17:21

标签: javascript jquery forms checkbox

我需要你的帮助,因为我不知道javascript或jquery库,我想我必须在我的项目中使用它们。我在页面加载时有一个禁用的表单,我想在我检查时启用复选框。我尝试了很多,但我没有找到任何解决方案..

这是'禁用'页面加载时的代码:

$(document).ready(function (e) {
  $("#form1 *").prop("disabled", "disabled");
});

复选框是:

<input type="checkbox" id="checkbox" name="opt[]" value="'.$points.'">

如果您有任何想法,请通知我!

一枪但是......没有结果是:

document.getElementById('checkbox').onchange = function() {
  document.getElementById('eksargirwsh').disabled = this.checked;
};

表单代码:

 <form method="post" id="eksargirwsh" action="update_points_test2.php">

           <div class="col-lg-4">
               <div class="ibox float-e-margins">
                    <div class="ibox-title">
                                    <h5>Form1</h5>

                                </div>
                                <div class="ibox-content">

echo '

                                    <div>
                                        <div class="feed-activity-list"><div style="border: 0.5px solid green; border-right-style:none;" class="input-group m-b"><span class="input-group-addon"> 
                                                <input type="checkbox" id="checkbox" name="opt[]" value="'.$points.'"></span>
                                                <input type="hidden" name="opt2[]" value="'.$row_select4['id'].'">


                                          <div class="feed-element">
                                                <a href="profile.html" class="pull-left">
                                            <img alt="image" class="img-circle" src="'. $row_select4['image_url']. '">
                                                </a>
                                          <div class="media-body ">
                                         <div class="ibox-tools">
                                                        <span class="label label-primary">ΔΙΑΘΕΣΙΜΟ</span><br><br>
                                                        <span class="label label-warning-light pull-right"><strong>'  .$row_select4['points'].  '</strong> Πόντοι</span>
                                                    </div>
                                                    <strong>'  .$row_select4['title'].  ' </strong> '   .$row_select4['description'].  ' <br>
                                                    <small class="text-muted">Διάρκεια: <strong>'  .$row_select4['start_date'].  ' - '   .$row_select4['end_date'].  ' </strong></small>
                                                    <div class="well">
                                                        '  .$row_select4['description'].  '
                                                    </div>
                                                  </div>
                                                </div>
                                            </div>

                                       </div>'  ;

 <button type="submit" id="submit" class="btn btn-w-m btn-primary">SUBMIT</button> 
                          </form>

2 个答案:

答案 0 :(得分:3)

使用它。您可以选择不是#checkbox的元素,然后首先禁用它们。然后,添加一个函数checkbox更改,如果选中则启用。

$(document).ready(function (e) {
  $("#form1 *:not(#checkbox)").prop("disabled", true);
  $("#form1").addClass('disabled');
  $('#checkbox').change(function(){
    $("#form1 *:not(#checkbox)").prop("disabled", !this.checked);
    $("#form1").toggleClass('disabled', !this.checked);
  })
});
#form1{
    opacity: 1;
}
#form1.disabled{
    opacity: 0.2;
}

答案 1 :(得分:1)

如果是

,则复选框的格式相同

除了

之外,你必须禁用整页
$(document).ready(function() {
       $('form :not([type=checkbox])').prop('disabled',true);

});

将此功能放入复选框

 $(":checkbox").click(function(){
    if (this.checked) {
       $('#form :not([type=checkbox])').prop('disabled',false);
    }
    else{
     $('#form :not([type=checkbox])').prop('disabled',true);
    }
}) ;

对于用户要求,下面创建的小提琴是链接

JSFiddle