2017-01-06 50 views
0

enter image description here如何在复选框作品,其生成的动态

下面给出的包括复选框,当父复选框被选中其所有子复选框也被选中,当选择单胎复选框,其父也被自动选中。

但是,当我添加新的复选框时,它不工作,当我检查新的孩子复选框,它的父母没有被选中。请帮忙。

的代码,如下所示:

这是add_checkbox.php

<!doctype html> 
<html> 
<head> 
    <title>Adding checkbox</title> 
    <link rel="stylesheet" type="text/css" href="test4.css" /> 
</head> 
<body> 
    <div id="maindiv"> 
    <br /><br /> 
    <div id="checkbox_div"> 
    <div id="checkbox_subdiv1"> 
    <p>Manage Permission</p> 
    </div> 
    <div id="subdiv2"> 
    <form action="#" method="POST" id="myform"> 
     <br /> 
     <select id="dropdown"> 
     <option>subsubfgh</option> 
     </select> 
     <br />  
     <ul> 
     <li><!--list of Property checkbox--> 
     <input type="checkbox" class="parent" name="Property" />Property 
     <ul> 
      <li id="Edit_Property"> 
      <input type="checkbox" class="child" name="Edit_Property" />Edit_Property 
      </li> 
      <li id="Remove_Property"> 
      <input type="checkbox" class="child" name="Remove_Property" />Remove_Property 
      </li> 
      <li id="Add_Property"> 
      <input type="checkbox" class="child" name="Add_Property" />Add_Property 
      </li> 
     </ul> 
     </li><!--end of Property checkbox--> 
     <li><!--list of Testimonial checkbox--> 
     <input type="checkbox" class="parent" name='Testimonial'/>Testimonial 
     <ul> 
      <li id="Add"> 
      <input type="checkbox" class="child" name="Add" />Add 
      </li> 
      <li id="Remove"> 
      <input type="checkbox" class="child" name="Remove" />Remove 
      </li> 
      <li id="View"> 
      <input type="checkbox" class="child" name="View" />View 
      </li> 
      <li id="Edit"> 
      <input type="checkbox" class="child" name="Edit" />Edit 
      </li> 
     </ul> 
     </li><!--end of testimonial checkbox--> 
     <li><!--list of users checkbox--> 
     <input type="checkbox" class="parent" name='Users'/>Users 
     <ul> 
      <li id="Edit_User"> 
      <input type="checkbox" class="child" name="Edit_User" />Edit_User 
      </li> 
      <li id="View_User_List"> 
      <input type="checkbox" class="child" name="View_User_List" />View_User_List 
      </li> 
      <li id="Add_User"> 
      <input type="checkbox" class="child" name="Add_User" />Add_User 
      </li> 
     </ul> 
     </li><!--end of users checkbox--> 
     <li><!--list of membership checkbox--> 
     <input type="checkbox" class="parent" name='Membership'/>Membership 
     <ul> 
      <li id="Edit_Membership"> 
      <input type="checkbox" class="child" name="Edit_Membership" />Edit_Membership 
      </li> 
      <li id="Remove_Membership"> 
      <input type="checkbox" class="child" name="Remove_Membership" />Remove_Membership 
      </li> 
      <li id="Add_Membership"> 
      <input type="checkbox" class="child" name="Add_Membership" />Add_Membership 
      </li> 
     </ul> 
     </li><!--end of membership checkbox--> 
     </ul> 
    </form> 
    </div> 
    </div> 
    </div> 
    <div id="form_div"> 
    <br /><br /> 
    <div id="form_sub_div1"> 
    <br /><br /> 
    <form action="test4.php" method="POST"> 
    Parent:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    <select id="select_parent" name="select_parent"> 
     <option>Property</option> 
     <option><p>Edit_Property</p></option> 
     <option><p>Remove_Property</p></option> 
     <option><p>Add_Property</p></option> 
     <option>Testimonial</option> 
     <option><p>Add</p></option> 
     <option><p>Remove</p></option> 
     <option><p>View</p></option> 
     <option><p>Edit</p></option> 
     <option>Users</option> 
     <option>Edit_User</option> 
     <option>View_User List</option> 
     <option>Add_User</option> 
     <option>Membership</option> 
     <option>Edit_Membership</option> 
     <option>Remove_Membership</option> 
     <option>Add_Membership</option> 
    </select> 
    <br /><br /> 
    Add New Checkbox: 
    <input type="text" name="input_checkbox" /> 
    <br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    <input type='button' value="Add" id="add_button" /> 
    <span id="demo"></span> 
    </form> 
    </div> 
    </div> 
</body> 
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
<script type="text/javascript" src="test4.js"></script> 
</html> 

这是addcheckbox.js

$(document).ready(function() { 
    $('input:button').on('click', function() { 

     // get the name of the parent selected from the dropdown 
     var chosen_parent = $('#select_parent option:selected').text(); 

     // get text from 'Add New Checkbox' textbox 
     var child_name = $(':text').attr("name", "input_checkbox").val(); 

     // create a checkbox to append under the parent checkbox 
     var temp_checkbox = '<li><input type="checkbox" id=id_'+child_name+' name=' + child_name +'>' + child_name + '</li>'; 

     // appending the checkbox under the selected parent 
     $(':checkbox.parent').filter(function() { 
      if ($(this).attr("name") === chosen_parent) { 
       $(this).next('ul').append(temp_checkbox); 
      } 
     }); 


     $(':checkbox.child').filter(function() { 
      if ($(this).attr("name") === chosen_parent) { 
       $('#'+chosen_parent).append('<ul>'+temp_checkbox+'</ul>'); 
      } 
     }); 
    }); 
$(function() { 
    $("li:has(li) > input[type='checkbox']").change(function() { 
    $(this).siblings('ul').find("input[type='checkbox']").prop('checked', this.checked); 
    }); 

$("input[type='checkbox'] ~ ul input[type='checkbox']").change(function() { 
    $(this).closest("li:has(li)").children("input[type='checkbox']").prop('checked', $(this).closest('ul').find("input[type='checkbox']").is(':checked')); 
    }); 
}) 
}) 

这是add_checkbox.css

#maindiv{width:100% height:700px; margin:auto;} 
#checkbox_div{width:250px; height:100%; float:left; background-color:gray; border:1px solid black;} 
#checkbox_subdiv1{width:250px; height:7%; margin:auto; border:1px solid black;} 
input[type="checkbox"]{cursor:pointer;} 
    #dropdown{margin-left:17%; cursor:pointer;} 
    p{position:relative; left:40px; top:1%;} 
    ul li{list-style-type:none;} 
#form_div{background-color:lightgreen; width:1018px; height:463px; float:left;} 
    #form_sub_div1{background-color:lightyellow; width:350px; height:180px; margin:auto; border:1px solid black;} 
    #select_parent{cursor:pointer;} 
    #add_button{cursor:pointer;} 
+0

[?在jQuery中,如何将事件附加到动态HTML元素(HTTP的可能重复://计算器。 com/questions/1359018/in-jquery-how-to-attach-events-to-dynamic-html-elements) – vijayP

回答

2

使用de legated事件是这样的:

$(document).on('click', 'input:button', function() {}); 

$(document).on('change', "li:has(li) > input[type='checkbox']", function() {}); 

$(document).on('change', "input[type='checkbox'] ~ ul input[type='checkbox']", function() {}); 

您还可以查看文档上http://api.jquery.com/on/

。对(事件[,选择] [数据])

直接和委托事件
当提供选择器时,事件处理程序被称为 委托。当事件直接发生在绑定元素的 上时,不会调用处理函数,但仅对于 与选择器匹配的后代(内部元素)。 jQuery将事件从事件目标 中的事件冒泡到处理程序所附的元素(即,最内层到最外层元素 ),并为匹配选择器的路径上的任何元素运行处理程序。

+0

仍然无法正常工作 – Birju

+0

@Birju对不起,我忘了添加两个组合,我建议你阅读关于代理活动,这可能会帮助你在未来的这类问题 –

1

尝试下面的复选框上安装change事件事件委托技术的方式:

$(function() { 
    $(document).on("change", "li:has(li) > input[type='checkbox']", function() { 
    $(this).siblings('ul').find("input[type='checkbox']").prop('checked', this.checked); 
    }); 

$(document).on("change","input[type='checkbox'] ~ ul input[type='checkbox']",function() { 
    $(this).closest("li:has(li)").children("input[type='checkbox']").prop('checked', $(this).closest('ul').find("input[type='checkbox']").is(':checked')); 
    }); 
}) 
+0

非常感谢。它现在真的有用 – Birju

+0

这就是伟大的@Birju ...! – vijayP

相关问题