首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >checkbox没有值php

checkbox没有值php
EN

Stack Overflow用户
提问于 2017-09-26 03:52:09
回答 1查看 32关注 0票数 0

我正在尝试将输入内容转换为表单上的复选框。选中时,我希望值为1,未选中时为0。我尝试了一些东西,但在提交时,值不会添加到数据库中。

HTML-1:

代码语言:javascript
复制
<div class="col-sm-5 col-sm-push-1 form-group required">
                        <label class="control-label" for="input-active"><?php echo $entry_active; ?></label>
                          <div class="input-active required">
                          <input type="checkbox" name="active" value="0" placeholder="<?php echo $text_active; ?>" id="input-active" class="form-control"   />
                        </div>
                        <?php if (isset($error_date_starting)) { ?>
                          <label class="text-danger"><?php echo $error_code; ?></label>
                        <?php } ?>
                      </div>ut type="checkbox" name="active" value="0" placeholder="<?php echo $text_active; ?>" id="input-active" class="form-control"   />
                        </div>
                        <?php if (isset($error_date_starting)) { ?>
                          <label class="text-danger"><?php echo $error_code; ?></label>
                        <?php } ?>
                      </div>

HTML-2:

代码语言:javascript
复制
<div class="col-sm-5 col-sm-push-1 form-group required">
                        <label class="control-label" for="input-active"><?php echo $entry_active; ?></label>
                          <div class="input-active required">
                          <input type="hidden" name='active' value='0' placeholder="<?php echo $text_active; ?>" id="input-active" class="form-control"   />
                          <input type="checkbox" name='active' value='1' placeholder="<?php echo $text_active; ?>" id="input-active" class="form-control"   />
                        </div>
                        <?php if (isset($error_date_starting)) { ?>
                          <label class="text-danger"><?php echo $error_code; ?></label>
                        <?php } ?>
                      </div>

我尝试了一些Javascripts解决方案,但都不起作用:

代码语言:javascript
复制
<script type="text/javascript">
$('#input-active').on('change', function(){
   this.value = this.checked ? 1 : 0;
   // alert(this.value);
}).change();
</script>

更新!!仍然不能工作,我已经按照建议修改了代码,仍然没有做我想做的事情。

新的HTML:

代码语言:javascript
复制
<div class="col-sm-5 col-sm-push-1 form-group required">
                        <label class="control-label" for="input-active"><?php echo $entry_active; ?></label>
                          <div class="input-active required">

                          <input type="checkbox" name='active' value='0' placeholder="<?php echo $text_active; ?>" id="input-active" class="form-control"   />
                        </div>
                        <?php if (isset($error_date_starting)) { ?>
                          <label class="text-danger"><?php echo $error_code; ?></label>
                        <?php } ?>
                      </div>

新的JS:

代码语言:javascript
复制
 <script type="text/javascript">
    $("#input-active").change(function() {
      if(this.checked) {
          $("#input-active").val('1');
      } else {
          $("#input-active").val('0');
          }
  });
  </script>
EN

回答 1

Stack Overflow用户

发布于 2017-09-26 03:55:32

要在输入类型上捕获切换的事件,请使用以下函数

代码语言:javascript
复制
$("#input-active").change(function() {
  if($(this).is(':checked')) {
    console.log('checked')
    $(this).val(1);
  } else {
    console.log('un checked')
    $(this).val(0);
  }
  
  console.log($(this).val())
});
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-sm-5 col-sm-push-1 form-group required">
  <label class="control-label" for="input-active">In active</label>
    <div class="input-active required">

    <input type="checkbox" name='active' value='0' placeholder="Active" id="input-active" class="form-control"   />
  </div>
</div>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46413137

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档