问题
I'm trying to set the checked property of checkboxes with jQuery, the jQuery code is working fine, but I got the problem in codebehind:
particular.click(function () {
company.removeAttr('checked');
particular.attr('checked', 'true');
});
Company and particular are the names of checkboxes, the jQuery code is working fine, my problem is in codebehind (.cs file)
if (particular.Checked)
{
company_name_blank.EnableClientScript = false;
cif_blank.EnableClientScript = false;
}
This is not working because Checked property is set on false when actually I'm setting it on true as you can see in the jQuery code, so where is the mistake?? For more explanation, I'm trying to disable some RequiredFieldValidators depending on which checkbox is checked, but as I told, Checked property is always set on false unless I set it on true by default in the element Checked=true, or particular.Checked=true in codebehind file, but that is not what I want to. I want to set on true the Checked property when I click the checkbox that's why I used jQuery.
UPDATE
My code
protected void Page_Load(object sender, EventArgs e)
{
if (particular.Checked)
{
company_name_blank.EnableClientScript = false;
cif_blank.EnableClientScript = false;
contact_name_blank.EnableClientScript = false;
contact_cognames_blank.EnableClientScript = false;
}
}
回答1:
In your website you will have a button like this:
<asp:Button runat="server" ID="btnClick" OnClick="btnClick_Click" Text="Click here" />
In your code behind you should have something like this:
protected void btnClick_Click(object sender, EventArgs e)
{
if (particular.Checked)
{
company_name_blank.EnableClientScript = false;
cif_blank.EnableClientScript = false;
contact_name_blank.EnableClientScript = false;
contact_cognames_blank.EnableClientScript = false;
}
}
You can remove the code in Page_Load and move it to the button click function
UPDATE - Correct solution for question
The correct solution is using the ValidationEnable Javascript function to disable/enable RequiredFieldValidators.
Code for this solution:
var emailEmpty = $("#registerContent").find('[id$=email_blank]'); //This is a jQuery object
ValidationEnable(emailEmpty[0], false); //This will disable the validator.
The reason we use "[0]" is because we need the DOM Element that is inside the jQuery object (see: http://api.jquery.com/get/?rdfrom=http%3A%2F%2Fdocs.jquery.com%2Fmw%2Findex.php%3Ftitle%3DCore%2Fget%26redirect%3Dno)
回答2:
Try to use :checked filter
particular.is(':checked')
来源:https://stackoverflow.com/questions/17877866/checking-checkbox-with-jquery