删除复选框的值

时间:2012-12-06 01:18:26

标签: javascript html checkbox

我的问题是: 我找到了一些代码。我可以做什么来运行我所说的东西?我想当有人点击菜单项时,它会删除复选框值。(未选中)我的代码是这样的:

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js" type="text/javascript"></script>
<script type="text/javascript"> 

$(function(){
    $(".form-row").hide();

       $("#divListBox").find(":checked").each(function() {
   $(this).removeAttr("checked");
});


       $("#course_english").change(function() {
         if ($("#course_english").val() == "MA English") {
             $(".form-row").show();
         } else {
            $(".form-row").hide();
         }
    });
 });
</script>
</head>
<body>
<select id="course_english" class="" name="course_english"> 
    <option value="">--Select One--</option> 
    <option value="MA English">MA English</option> 
    <option value="B. A. (Hons.) with Mass Communication">B.A. (Hons.) with Mass Communication</option> 
    <option value="M. A. (English)">M. A. (English)</option> <option value="M. Phil.">M. Phil.</option> 
</select>


  <div class="form-row" id="divListBox">
    <label for="sem_1_ma_english-poetry">
      <input id="sem_1_ma_english-poetry" class="sem_1_ma_english" type="checkbox"      value="Poetry" name="sem_1_ma_english[]">
Poetry
 </label>
 <label for="sem_1_ma_english-drama">
  <input id="sem_1_ma_english-drama" class="sem_1_ma_english" type="checkbox"     value="Drama"  name="sem_1_ma_english[]">
Drama
</label>
 <label for="sem_1_ma_english-fiction">
  <input id="sem_1_ma_english-fiction" class="sem_1_ma_english" 
   type="checkbox" value="Fiction" name="sem_1_ma_english[]">
  Fiction
 </label>
 <label for="sem_1_ma_english-prose">
 <input id="sem_1_ma_english-prose" class="sem_1_ma_english" type="checkbox"      value="Prose" name="sem_1_ma_english[]">
 Prose
</label></div>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

尝试:

   $("#course_english").change(function() {
     if ($("#course_english").val() == "MA English") {
         $(".form-row").show();
     } else {
        $(".form-row").hide();
     }
    $("#divListBox").find(":checked").each(function() {
        $(this).prop("checked", false);
    });
});

答案 1 :(得分:0)

试试看:

$("#divListBox").find(":checked").each(function() {
   $(this).removeAttr("checked");
});

在$(“#course_english”)。change()内,这将确保在选择值更改时取消选中每个框。

最终的js将是:

$(function(){
    $(".form-row").hide();




       $("#course_english").change(function() {
         $("#divListBox").find(":checked").each(function() {
           $(this).removeAttr("checked");
         });

         if ($("#course_english").val() == "MA English") {
             $(".form-row").show();
         } else {
            $(".form-row").hide();
         }

    });
 });

DEMO HERE

相关问题