jquery click a checkbox that is hidden
I'm trying to make a color filter for a website. Where I want to remove the visible checkbox (i got that working), remove the labels etc. And only leave a div with a background color <-- this all works. But now I don't know how to make these div's click able since the input field is on hidden. Is there a way to fix this in jQuery?
Here's my code:
function onDataReceived(data) {
$.each(data.colorInfo, function(key, colorInfo) {
var inputElement = $("input[name^=filter][value="+colorInfo.id+"]").css("display", "none");
var labelElement = $('label[for^=filter]').remove();
var div = inputElement.parent();
div.attr('class', 'myclass_'+colorInfo.id);
div.css("background-color", colorInfo.colorCode);
div.css("width", "15px");
div.css("height", "15px");
div.css("display", "inline-block");
div.css("marginLeft", "5px");
div.css("marginBottom", "5px");
//console.log(colorInfo);
});
}
$(document).ready(function () {
var url = 'myapi.json';
$.get(url, onDataReceived);
});
And the HTML:
<div class="filter_3">
<input name="filter[]" value="152194" type="checkbox"/>
<label for="filter_152194">Rood</label>
</div>
<div class="filter_4">
<input name="filter[]" value="152196" type="checkbox"/>
<label for="filter_152196">Blauw</label>
</div>
<div class="filter_5">
<input name="filter[]" value="152198" type="checkbox"/>
<label for="filter_152198">Oranje</label>
</div>
假设一旦div被点击,就有一个函数被调用:
function onDataReceived(data) {
$.each(data.colorInfo, function(key, colorInfo) {
var inputElement = $("input[name^=filter] [value="+colorInfo.id+"]").css("display", "none");
var labelElement = $('label[for^=filter]').remove();
var div = inputElement.parent();
div.attr('class', 'myclass_'+colorInfo.id);
div.css("background-color", colorInfo.colorCode);
div.css("width", "15px");
div.css("height", "15px");
div.css("display", "inline-block");
div.css("marginLeft", "5px");
div.css("marginBottom", "5px");
/* Added Lines */
div.css("pointer-events", "auto"); //Just in case, but it's probably not necessary
div.css("cursor", "pointer"); //It's nice to have
div.on('click', function() { //This is a click event delgated to the div
// Whatever code is needed when div is clicked
});
//console.log(colorInfo);
});
}
$(document).ready(function () {
var url = 'myapi.json';
$.get(url, onDataReceived);
});
链接地址: http://www.djcxy.com/p/56026.html
上一篇: 如何创建数组在MVC中使用选中的ID
下一篇: jquery点击一个隐藏的复选框