-2

产品.aspx

    <asp:DropDownList id="ddlProduct" runat="server" CssClass="txtbox">
    <asp:ListItem Value="" Selected="True">Select</asp:ListItem>
    <asp:ListItem Value="Mac">Mac</asp:ListItem>
    <asp:ListItem Value="iPad">iPad</asp:ListItem>
    <asp:ListItem Value="iPhone">iPhone</asp:ListItem>
    <asp:ListItem Value="iPod">iPod</asp:ListItem>
    <asp:ListItem Value="Accessories">Accessories</asp:ListItem>
    </asp:DropDownList>
    <asp:CheckBox ID="CheckBox1" runat="server" />
    <asp:RadioButton ID="RadioButton1" runat="server" />
    <asp:Button ID="Button1" runat="server" onclick="clearForm(oForm)"/>

看到这个 javascript 代码:

      $(document).ready(function () {
       $("#form1").validate({



           rules: {
               ddlCcity: { required: true },
                ddlProduct: { required: true }
           }

       });

       $.validator.addMethod(
    "select_class",
    function (value, element) {
        return this.optional(element) || (value.indexOf("--Select One--") ==-1);
    },
    "Please select a class.");

   });


    function clearForm(oForm) {
    var elements = oForm.elements;
       oForm.reset();
    for (i = 0; i < elements.length; i++) {
       field_type = elements.type.toLowerCase();
       switch (field_type) {
          case "text":
           case "password":
            case "textarea":
             case "dropdownlist":
             elements.value = "";
                break;
                case "radio":
                case "checkbox":

                if (elements.checked) {

                    elements.checked = false;

                }

                break;
            case "select-one":
            case "select-multi":

                elements.selectedIndex = 0;

                break;



            default:

                break;

        }

    }
}
    </script>

在 Product.aspx 中,我使用了 validation.js(javascript 代码),但验证不起作用如何使用 javascript 代码验证下拉列表、复选框、单选按钮

4

1 回答 1

0

尝试以下

function validate(){
var i=0;
if($('#CheckBox1').is(":checked")==false){
i++;
}
if($('#RadioButton1').is(":checked")==false){
i++;
}
if($('#ddlProduct').val()==""){
i++;
}
if(i>0){
return false;
}else{
return true;
}
}
于 2013-03-06T04:57:29.013 回答