复选框而不是按钮

时间:2017-02-05 18:47:57

标签: javascript php jquery checkbox

首先,我对此有点新意见(但我学得很快)。我想将我的代码(下面)中的“完成按钮”更改为复选框。现在我有一个用于标记完成的按钮,另一个用于标记撤消的按钮和另一个用于删除的按钮。对于前2个我想要一个复选框(所以两者都标记为撤消)。尝试了一些事情(在代码中你看到我从这个页面得到的jquery代码来获取复选框的id),这段代码的问题是我只得到数据库中第一个复选框/项的id,而不是第二个,3,...... 如果你能给我一个获得身份证的解决方案,我会很高兴。 如果你能给我改变完成按钮的解决方案(重新路由到mark.php网站),我会很高兴。 如果你能给我两个解决方案,我会在你的部门,你会得到我的赞许: - )

<script>
$(document).ready(function() {
  $('#id').on('change', function(event) {
    var checkbox = $(event.target);
    var isChecked = $(checkbox).is(':checked');
    alert('checkbox ' + checkbox.attr('value') + ' is checked: ' + isChecked);
  });
});
</script>

<?php
$userId = session::get(config::get('session/session_name'));
$items = DB::getInstance()->get('items', array('user', '=', $userId));

?>

<div class="list">
      <h1 class="header">My Page</h1>

  <?php if($items->count()): ?>
    <ul class="items">
  <?php foreach($items->results() as $item): ?>
        <li>
          <input type="checkbox" id="id" value="<?php echo $item->id; ?>" <?php echo $item->done ? ' checked' : '' ?>  />
          <span class="item<?php echo $item->done ? ' done' : '' ?>"><?php echo $item->name; ?></span>
          <?php if(!$item->done): ?>
                <a href="mark.php?as=done&item=<?php echo $item->id; ?>" class="done-button">Gedaan</a>
          <?php endif; ?>
          <?php if($item->done): ?>
                <a href="mark.php?as=notdone&item=<?php echo $item->id; ?>" class="done-button">Niet gedaan</a>
          <?php endif; ?>
          <?php if($item->done): ?>
                <a href="mark.php?as=delete&item=<?php echo $item->id; ?>" class="done-button">Verwijderen</a>
          <?php endif; ?>
        </li>
  <?php endforeach; ?>
    </ul>
  <?php else: ?>
    <p>Je hebt nog niets toegevoegd</p>
  <?php endif;?>

  <form class="item-add" action="add.php" method="post">
    <input type="text" name="name" placeholder="Hier iets typen" class="input" autocomplete="off" required>
    <input type="submit" class="submit" value="Toevoegen">
  </form>

1 个答案:

答案 0 :(得分:1)

你的问题是:为什么我只能处理#id,isn&#t; t?

试试这个:

选项1:

$('body').on('change','#id', function(e) {
    if($(this).is(':checked')) {
        alert('checkbox with value ' + $(this).val() + ' is checked');
    } else {
        alert('checkbox with value ' + $(this).val() + ' is unchecked');
    }
});

DEMO:http://jsbin.com/vumokab

选项2:

$('input[id="id"]').change( function(e) {
    if($(this).is(':checked')) {
        alert('checkbox with value ' + $(this).val() + ' is checked');
    } else {
        alert('checkbox with value ' + $(this).val() + ' is unchecked');
    }
});

DEMO:http://jsbin.com/cozano

相关问题