【问题标题】:How to check the value of CheckBoxList by Javascript如何通过 Javascript 检查 CheckBoxList 的值
【发布时间】:2013-04-19 02:38:02
【问题描述】:
--------------------------------------------
Phone number  : ________________________
Email Address : ________________________
You prefer to receive the notification by:
[ ] SMS [ ] Email 
--------------------------------------------

这是我在 .aspx 页面中的内容(我无法发布图片)、两个文本框和两个复选框。这些复选框是数据绑定复选框,其中“SMS”和“Email”是当前数据库中的两个可用值。

        ckBxPreferredMethod.DataSource = dt;
        ckBxPreferredMethod.DataTextField = "Text";
        ckBxPreferredMethod.DataValueField = "Value";
        ckBxPreferredMethod.DataBind();

我现在想要做的是,使用 customvalidator 和 JavaScript(客户端验证),我想确保当且仅当用户检查 SMS 和电子邮件复选框时,他们必须分别填写他们的电话号码和电子邮件地址。如果他们未选中 SMS,则“电话号码”是一个可选字段。

我更擅长 C#,就像我使用 JavaScript 一样,所以我使用以下方法在服务器端完成了这项工作,没有问题:

        protected void ckBxPreferredMethod_ServerValidate(object sender, ServerValidateEventArgs e)
    {
        if(ckBxPreferredMethod.SelectedValue == "1" && String.IsNullOrEmpty(txtHPNo.Text.Trim()))
        {
            e.IsValid = false; 
            CVemailAndSMS.ErrorMessage = "You need to enter your phone number if you want to receive by SMS"; 
        }

        if(ckBxPreferredMethod.SelectedValue == "2" && String.IsNullOrEmpty(txtEmail.Text.Trim())) 
        {
            e.IsValid = false; 
            CVemailAndSMS.ErrorMessage = "You need to enter your email if you want to receive by email"; 
        }
    }

上述方法效果很好。但由于我的程序进一步复杂化,我将不得不移动这个客户端。所以 JavaScript 是必需的。

如果这涉及静态复选框,我不会有问题。但是对于数据绑定的复选框列表,我只是迷失了如何检查所选复选框的值。

谢谢。

【问题讨论】:

  • 你的问题有点混乱。因为您的数据绑定到文本字段,而不是复选框。您可以随时使用 html 复选框和 javascript 来检查它们是否被选中。

标签: c# javascript asp.net webforms checkboxlist


【解决方案1】:

您需要添加更多信息才能获得合适的答案。

尝试一下 我认为您想开始研究使用 JavaScript 的条件验证

@SEE:http://www.users.muohio.edu/klumbca/examples/conditional-validation-example.html?hearabout=Newspaper+Ad&hearabout_other=

注意如果你标记了“其他”,你只需要输入文本吗?

不保证使用的插件,但它应该为您指明正确的方向。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 2011-03-30
    • 2013-12-21
    • 1970-01-01
    • 1970-01-01
    • 2012-03-22
    • 2011-09-13
    相关资源
    最近更新 更多