jquery is unable to set the prettyCheckable plugin radio button programmatically
I am using the prettyCheckable jquery plugin to make my checkboxes and radio buttons look pretty and if I use jquery to set the property checked it does not work because the plugin hides the actual checkbox and makes it's own override html to pretty up the checkbox/radio, therefore the jquery doesn't recognize when I check the radio or checkbox
//on button click i tried all the things. but nithing works. please help
<input type="radio" id="rdTypeAdd" name="taxationtype" class="radiobutton" value="+" data- label="Additive" data-customclass="margin-right" checked="checked" />
<input type="radio" id="rdTypeSub" name="taxationtype" class="radiobutton" value="-" data-label="Subtractive" data-customclass="margin-right" />
$('#rdTypeAdd').buttonset();
$('[name="rdTypeAdd"][value="+"]').prop("checked", true);
$('#rdTypeAdd').buttonset("refresh");
//$('[name="rdTypeAdd"][value="+"]').attr("checked", true);
//$('[name="rdTypeAdd"][value="+"]').prop("checked", true).trigger("change");
//($('.rdTypeAdd').prop('checked',true));
Not sure how much of a hack this is but I was able to get a div to check and uncheck a prettyCheckable checkbox. I noticed when I removing the class="checked" from the that prettyCheckable includes the checkbox became unchecked.
$("#checkbox").prettyCheckable();
$("#checkboxDiv").click(function() {
if ($("#checkbox").is(':checked'))
{
$("#checkbox").next().removeClass("checked");
$('#checkbox').each(function(){ this.checked = false; });
} else {
$("#checkbox").next().addClass("checked");
$('#checkbox').each(function(){ this.checked = true; });
}
});
Adding or removing the "checked" class from the (which is .next() to the prettyCheckable element) and setting checked=true in the original checkbox element are both needed for it to work. Yet my if cookie is set piece is able to check the box with only the 's class. Like I said I don't know how bad of a hack this is but it worked for me hopefully it helps you out. Not sure why prettyCheckable doesn't have this ability or atleast document it because I could not find anything.
You can use pretty checkable functions:
$('[name="rdTypeAdd"][value="+"]').prettyCheckable('check');
and
$('[name="rdTypeAdd"][value="+"]').prettyCheckable('uncheck');
这个答案建立在@ kravits88和这个github问题上
var $radio = $('[name="rdTypeAdd"][value="+"]');
$radio.prettyCheckable('check');
$radio.prop('checked', true).change();
$radio.click();
链接地址: http://www.djcxy.com/p/56048.html
上一篇: 如何根据变量设置复选框