如果在提交按钮时检查特定复选框的值,如何获取该值?

时间:2016-10-03 09:00:43

标签: php jquery

我有两个包含相同名称,类和ID的复选框的div。当我选择特定div的复选框时,它应该在提交按钮时提醒该复选框的值。

示例:

<div>

    <input type="checkbox" class="snowplay" id="snowplayy" name="snowplay" value="1">
</div>
<div>

    <input type="checkbox" class="snowplay" id="snowplayy" name="snowplay" value="2">
</div>
<div>

    <input type="submit" class="submit" id="submit" name="submit" />
</div>

当我点击两个复选框中的任意一个,然后提交按钮时,我想提醒该特定复选框的复选框的值。谁能说怎么做呢?

2 个答案:

答案 0 :(得分:1)

$('.submit').click(function(e) {
  e.preventDefault()
    //alert($('.snowplay:checked').val()) //single value
  $('.snowplay:checked').each(function() {//iterate over 
    alert($(this).val()) // many value

  })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>

  <input type="checkbox" class="snowplay" id="snowplayy" name="snowplay" value="1">
</div>
<div>

  <input type="checkbox" class="snowplay" id="snowplayy1" name="snowplay" value="2">

</div>

<div>

  <input type="submit" class="submit" id="submit" name="submit" />

</div>

  1. 将此.snowplay:checked复选框与已选中的班级雪玩
  2. 一起使用
  3. 将您的ID更改为唯一

答案 1 :(得分:0)

我应该是唯一的。所以在这里我删除了id属性:)。

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('#submit').click(function(){
$('.snowplay:checked').each(function(){
alert($(this).val())
});

});

});
</script>
</head>
<body>
<div>

    <input type="checkbox" class="snowplay"  name="snowplay" value="1">
</div>
<div>

    <input type="checkbox" class="snowplay"  name="snowplay" value="2">
</div>
<div>

    <input type="submit" class="submit" id="submit" name="submit" />
</div>

</body>
</html>