asp:textbox未捕获TypeError:无法读取未定义

时间:2016-11-17 10:12:09

标签: javascript c# jquery

我收到此错误,因此我的更新按钮不起作用,我没有更新我的数据库进度,因为这个错误。我想看到文本框,我尝试在.cs端

这个使用复选框和文本框的javascript代码使用json

发送到cs端

        function UpdateDetails1() {

            var bEnabled = GetCheckBoxVal($("#<%=CB_Enabled_Edit.ClientID%>"));
            var bDisplayDetails = GetCheckBoxVal($("#<%=CB_DisplayDetails_Edit.ClientID%>"));
            var b3rdParty = GetCheckBoxVal($("#<%=CB_b3rdParty_Edit.ClientID%>"));
            var bDisplayOpenNow = GetCheckBoxVal($("#<%=CB_DisplayOpenNow_Edit.ClientID%>"));
            var bReservation = GetCheckBoxVal($("#<%=CB_Reservation_Edit.ClientID%>"));
            var bPromotion = GetCheckBoxVal($("#<%=CB_Promotion_Edit.ClientID%>"));
            var bOnlyPromotion = GetCheckBoxVal($("#<%=CB_OnlyPromotion_Edit.ClientID%>"));
            var bAllowFollow = GetCheckBoxVal($("#<%=CB_AllowFollow_Edit.ClientID%>"));
            var bAlacarte = GetCheckBoxVal($("#<%=CB_Alacarte_Edit.ClientID%>"));
            var bDisplayOpenEat = GetCheckBoxVal($("#<%=CB_DisplayOpenEat_Edit.ClientID%>"));
            var Coord_Lat = $("#<%=TB_Coord_Lat_Edit.ClientID%>")[0].value;
            var Coord_Long = $("#<%=TB_Coord_Long_Edit.ClientID%>")[0].value;
            var Price = $("#<%=TB_Price_Edit.ClientID%>")[0].value;
            alert(Price);
            var Phone_Number = $('#<%=TB_Phone_Number_Edit.ClientID%>')[0].value;
            if (bReservation == true)
                $("#display_res_schedule").css("display", "block");
            else
                $("#display_res_schedule").css("display", "none");

            var jsonData = '{ bEnabled: "' + bEnabled + '",' +
                    'bDisplayDetails: "' + bDisplayDetails + '",' +
                    'b3rdParty:  "' + b3rdParty + '",' +
                    'Price:  "' + Price + '",' +
                    'bDisplayOpenNow: "' + bDisplayOpenNow + '",' +
                    'bReservation: "' + bReservation + '",' +
                    'bPromotion: "' + bPromotion + '",' +
                    'Phone_Number: "' + Phone_Number + '",' +
                    'bOnlyPromotion: "' + bOnlyPromotion + '",' +
                    'bAllowFollow: "' + bAllowFollow + '",' +
                    'bAlacarte: "' + bAlacarte + '",' +
                    'bDisplayOpenEat: "' + bDisplayOpenEat + '",' +
                    'Coord_Lat: "' + Coord_Lat + '",' +
                    'Coord_Long: "' + Coord_Long + '" }';
            
            alert(jsonData);
           // var jsonData = '{ bEnabled: "' + bEnabled + '",' +
           //       'bDisplayOpenNow: "' + bDisplayOpenNow + '",' +
           //         'bReservation: "' + bReservation + '",' +
           //         'Coord_Lat: "' + Coord_Lat + '",' +
           //         'Coord_Long: "' + Coord_Long + '" }';

            $.ajax({
                type: "POST",
                url: "Edit.aspx/UpdateDetails1",
                data: jsonData,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: GetDetails1,
                failure: function (response) {
                    alert(response.d);
                }
            });
        }

我需要做一些我不使用的可见错误复选框和文本框。但是TB_Phone_Number_Edit.Visible获取未捕获的TypeError:无法读取未定义错误的属性“值”

 protected void Page_Load(object sender, EventArgs e)
    {
        LoadType = Request.QueryString["t"].ToString();
        if (LoadType == "Agency")
        {
            CB_DisplayOpenNow.Visible = false;
            CB_DisplayOpenNow_Edit.Visible = false;
            CB_Alacarte.Visible = false;
            CB_Alacarte_Edit.Visible = false;
            CB_DisplayOpenEat.Visible = false;
            CB_DisplayOpenEat_Edit.Visible = false;
            CB_Reservation.Visible = false;
            CB_Reservation_Edit.Visible = false;
            CB_Promotion.Visible = false;
            CB_Promotion_Edit.Visible = false;
            CB_OnlyPromotion.Visible = false;
            CB_OnlyPromotion_Edit.Visible = false;
            TB_Price_Edit.Visible = false;
            DDL_Currency.Visible = false;
            DDL_Camera.Visible = false;
            CB_AllowFollow.Visible = false;
            CB_AllowFollow_Edit.Visible = false;
            WhatDoNow.Visible = false;
            Alacarte.Visible = false;
            WhatEatNow.Visible = false;
            Promotion.Visible = false;
            Reservation.Visible = false;
            OnlyPromotion.Visible = false;
            Price.Visible = false;
            Camera.Visible = false;
            AllowFollow.Visible = false;
            Label23.Visible = false;
            Label24.Visible = false;
            Label25.Visible = false;
            Label26.Visible = false;
            Label27.Visible = false;
            Label29.Visible = false;
            Label30.Visible = false;
            Label33.Visible = false;
            Label34.Visible = false;
            TB_Price_Edit.Visible = false;
        }
        else if(LoadType == "Tour"){
            CB_b3rdParty.Visible = false;
            CB_b3rdParty_Edit.Visible = false;
            CB_Alacarte.Visible = false;
            CB_Alacarte_Edit.Visible = false;
            DDL_Camera.Visible = false;
            CB_AllowFollow.Visible = false;
            CB_AllowFollow_Edit.Visible = false;
            CB_DisplayOpenEat.Visible = false;
            CB_DisplayOpenEat_Edit.Visible = false;
            b3rdParty.Visible = false;
            Alacarte.Visible = false;
            WhatEatNow.Visible = false;
            Camera.Visible = false;
            AllowFollow.Visible = false;
            Label18.Visible = false;
            Label24.Visible = false;
            Label25.Visible = false;
            Label33.Visible = false;
            Label34.Visible = false;
            Phone_Number.Visible = false;
            TB_Phone_Number_Edit.Visible = false;
            Label8.Enabled = false;

        }

2 个答案:

答案 0 :(得分:0)

将文本框设置为Visible = false会使其无法在html中呈现。因此,当您尝试获取javascript中的值时,它实际上不存在,使其成为undefined

要解决此问题,请使用隐藏字段替换文本框或更改不透明度,以便文本框在那里但是透明。

答案 1 :(得分:0)

假设问题不在GetCheckBoxVal以下任何一种陈述导致您的问题:

var Coord_Lat = $("#<%=TB_Coord_Lat_Edit.ClientID%>")[0].value;
var Coord_Long = $("#<%=TB_Coord_Long_Edit.ClientID%>")[0].value;
var Price = $("#<%=TB_Price_Edit.ClientID%>")[0].value;
var Phone_Number = $('#<%=TB_Phone_Number_Edit.ClientID%>')[0].value;

为什么呢?因为如果给jQuery的css选择器没有返回匹配,那么数组将具有零元素,也就是说,它的长度将为0.这意味着即使在元素[0]上也没有,它的值是未定义的。尝试获取属性或对未定义的值调用方法会给出您看到的异常。

正如建议的duplicate建议你应该防范这种情况。一种可能的方法是引入效用函数:

// this gets the value property of an object and if object is undefined
// returns a default
function getValueOrDefault(someObject, reasonableDefault) {

    return someObject === undefined ? reasonableDefault : someObject.value;
}

// your original function
function UpdateDetails1() { 
    // rest of your code
}

使用该功能,您的代码将变为:

var Coord_Lat = getValueOrDefault($("#<%=TB_Coord_Lat_Edit.ClientID%>")[0], '0.0');
var Coord_Long = getValueOrDefault($("#<%=TB_Coord_Long_Edit.ClientID%>")[0], '0.0');
var Price = getValueOrDefault($("#<%=TB_Price_Edit.ClientID%>")[0],0);
var Phone_Number = getValueOrDefault($('#<%=TB_Phone_Number_Edit.ClientID%>')[0],'+1 010');

那应解决Uncaught类型错误。由你来决定合理的默认值。