jquery bind event to one checkbox in array of checkboxes
I have a dynamic group of checkboxes pr_network_ids that is a HABTM relationship in RAILS. So they each have the same name, but different values
I want to toggle a div to be shown or hidden when pr_network_ids_2 and pr_开发者_运维知识库network_ids_4 is clicked/unclicked.
Using JQuery i have tried to access it using ("#pr_network_ids_2").click, but i can't get it to work. Is there a way to access a specific checkbox using the id or name or get the entire array of checkboxes and bind an event to the ones if there value equals 2 or 4?
Try this
$("input:checkbox[name=nameOfTheCheckbox]").each(function(){
var $this = $(this);
if($this.val() == "2" || $this.val() == "4"){
//bind the events here
$this.bind("click", function(){ });
}
});
If you use $("#pr_network_ids_2") you have to find the checkbox, if you have defined the checkbox with that id.
example
<input type="checkbox" id="pr_network_ids_2" value="lalala" />
Make sure Your checkbox has the correct ID.
("#pr_network_ids_2").click(...)
should work. But if they are dynamic, you either need to do the binding after they are created or use .live()
精彩评论