none
JS问题 RRS feed

  • 问题

  • <script type='text/javascript'>var IsCheckDateOfCtrId='';        $(function() {            SetCheckEmptyJS();        });function SetCheckEmptyJS () {
            $('#txtName').focusout(function () {            if ($('#txtName').val() == '') {                $('#txtName').attr('class', 'RequiredWarningTextBoxBorderStyle');            }            else {                $('#txtName').attr('class', 'tbox');            }            ClearLblError();        });
            $('#txtAddress').focusout(function () {            if ($('#txtAddress').val() == '') {                $('#txtAddress').attr('class', 'RequiredWarningTextBoxBorderStyle');            }            else {                $('#txtAddress').attr('class', 'tbox');            }            ClearLblError();        });
            $('#txtCorp').focusout(function () {            if ($('#txtCorp').val() == '') {                $('#txtCorp').attr('class', 'RequiredWarningTextBoxBorderStyle');            }            else {                $('#txtCorp').attr('class', 'tbox');            }            ClearLblError();        });
            $('#txtContact').focusout(function () {            if ($('#txtContact').val() == '') {                $('#txtContact').attr('class', 'RequiredWarningTextBoxBorderStyle');            }            else {                $('#txtContact').attr('class', 'tbox');            }            ClearLblError();        });
            $('#txtDesScale').focusout(function () {            if ($('#txtDesScale').val() == '') {                $('#txtDesScale').attr('class', 'RequiredWarningTextBoxBorderStyle');            }            else {                $('#txtDesScale').attr('class', 'tbox');            }            ClearLblError();        });
            $('#txtFacilities').focusout(function () {            if ($('#txtFacilities').val() == '') {                $('#txtFacilities').attr('class', 'RequiredWarningTextBoxBorderStyle');            }            else {                $('#txtFacilities').attr('class', 'tbox');            }            ClearLblError();        });
     
     
     
     
            $('#ddlCateagory').change(function () {            if ($(this).val() == '') {                $('#sp_ddlCateagory').attr('class', 'RequiredWarningDropDownListBorderStyle');            }            else {                $('#sp_ddlCateagory').attr('class', '');            }            ClearLblError();        });
     
     
                                                                                                $('#btnAdd').click(function () {            if ($('#txtName').val() == '') {                $('#txtName').attr('class', 'RequiredWarningTextBoxBorderStyle');               ClearLblError();            }            else {                $('#txtName').attr('class', 'tbox');            }            ClearLblError();
                if ($('#txtAddress').val() == '') {                $('#txtAddress').attr('class', 'RequiredWarningTextBoxBorderStyle');               ClearLblError();            }            else {                $('#txtAddress').attr('class', 'tbox');            }            ClearLblError();
                if ($('#txtCorp').val() == '') {                $('#txtCorp').attr('class', 'RequiredWarningTextBoxBorderStyle');               ClearLblError();            }            else {                $('#txtCorp').attr('class', 'tbox');            }            ClearLblError();
                if ($('#txtContact').val() == '') {                $('#txtContact').attr('class', 'RequiredWarningTextBoxBorderStyle');               ClearLblError();            }            else {                $('#txtContact').attr('class', 'tbox');            }            ClearLblError();
                if ($('#txtDesScale').val() == '') {                $('#txtDesScale').attr('class', 'RequiredWarningTextBoxBorderStyle');               ClearLblError();            }            else {                $('#txtDesScale').attr('class', 'tbox');            }            ClearLblError();
                if ($('#txtFacilities').val() == '') {                $('#txtFacilities').attr('class', 'RequiredWarningTextBoxBorderStyle');               ClearLblError();            }            else {                $('#txtFacilities').attr('class', 'tbox');            }            ClearLblError();
     
     
                if ($('#ddlCateagory').val() == '') {                $('#sp_ddlCateagory').attr('class', 'RequiredWarningDropDownListBorderStyle');               ClearLblError();            }            else {                $('#sp_ddlCateagory').attr('class', '');            }            ClearLblError();
     
            });
        
            $('#btnEdit').click(function () {            if ($('#txtName').val() == '') {                $('#txtName').attr('class', 'RequiredWarningTextBoxBorderStyle');               ClearLblError();            }            else {                $('#txtName').attr('class', 'tbox');            }            ClearLblError();
                if ($('#txtAddress').val() == '') {                $('#txtAddress').attr('class', 'RequiredWarningTextBoxBorderStyle');               ClearLblError();            }            else {                $('#txtAddress').attr('class', 'tbox');            }            ClearLblError();
                if ($('#txtCorp').val() == '') {                $('#txtCorp').attr('class', 'RequiredWarningTextBoxBorderStyle');               ClearLblError();            }            else {                $('#txtCorp').attr('class', 'tbox');            }            ClearLblError();
                if ($('#txtContact').val() == '') {                $('#txtContact').attr('class', 'RequiredWarningTextBoxBorderStyle');               ClearLblError();            }            else {                $('#txtContact').attr('class', 'tbox');            }            ClearLblError();
                if ($('#txtDesScale').val() == '') {                $('#txtDesScale').attr('class', 'RequiredWarningTextBoxBorderStyle');               ClearLblError();            }            else {                $('#txtDesScale').attr('class', 'tbox');            }            ClearLblError();
                if ($('#txtFacilities').val() == '') {                $('#txtFacilities').attr('class', 'RequiredWarningTextBoxBorderStyle');               ClearLblError();            }            else {                $('#txtFacilities').attr('class', 'tbox');            }            ClearLblError();
     
     
                if ($('#ddlCateagory').val() == '') {                $('#sp_ddlCateagory').attr('class', 'RequiredWarningDropDownListBorderStyle');               ClearLblError();            }            else {                $('#sp_ddlCateagory').attr('class', '');            }            ClearLblError();
     
            });
        
        };
     
    function ClearLblError()
                        {
                            if(document.getElementById('lblError')!=null)
                            {
                                document.getElementById('lblError').innerText = '';
                            }
                        }
                      </script>
    上面的是项目里有的js代码,可以看到dropdownlist(ddlcategory)验证的是空,现在我想点击按钮,注册ddlcategory事件,把
        if ($(this).val() == '')改成  if ($(this).val() == '-1'),代码怎么写?写在哪里?

    please verify my account

    2015年7月28日 4:32

答案

  • 
    上面的是项目里有的js代码,可以看到dropdownlist(ddlcategory)验证的是空,现在我想点击按钮,注册ddlcategory事件,把
        if ($(this).val() == '')改成  if ($(this).val() == '-1'),代码怎么写?写在哪里?

    你好,

    $(this).value()获取的是DropDownList当前选中项的value值,当前你的DropDownlist中有一项的value值为'',你只需要将该项的value值该为'-1',就可以将你的代码修改成上面那样。

    你可以参考下边的代码:

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script type='text/javascript'> $(document).ready(function () { $("#Button1").click(function () { var ddlValue = $("#DropDownList1").val(); if(ddlValue=="-1") { $("#div_output").text("请在下拉列表中选择一个值"); }

    else{

    $("#div_output").text("");

    } return false; }); }); </script> <div> <asp:DropDownList ID="DropDownList1" runat="server"> <asp:ListItem Text="Please select" Value ="-1"></asp:ListItem> <asp:ListItem Text="AAA" Value="101"></asp:ListItem> <asp:ListItem Text="BBB" Value="101"></asp:ListItem> <asp:ListItem Text="CCC" Value="101"></asp:ListItem> </asp:DropDownList> <asp:Button ID="Button1" runat="server" Text="Button" /> <div id="div_output"> </div> </div>

    在这个示例中,点击按钮时,如果DropDownList为第一项则会显示提示信息。

    2015年7月29日 9:35
    版主