2016-11-17 83 views
1

我得到這個錯誤,所以我的更新按鈕不工作,我沒有更新我的數據庫進度,因爲這個錯誤。我想顯示文本框,我嘗試在側面的.cs做asp:textbox Uncaught TypeError:無法讀取未定義的屬性'值'

使用checkboxs和textboxs這個JavaScript代碼發送到CS側JSON

 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獲得遺漏的類型錯誤:無法讀取未定義的錯誤

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; 

     } 
+0

我改變了這篇文章中的代碼,但是這個錯誤仍在繼續。我如何解決這個錯誤,我可以幫我嗎? – wotmn

回答

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'); 

應該可以解決未捕獲的類型錯誤。這是由你來提出合理的違約。

0

的特性「值」文本框設置爲Visible = false使得它不會在你的HTML渲染。所以當你試圖在你的javascript中獲得價值時,它實際上不存在,使它成爲undefined

要解決此問題,請將文本框替換爲隱藏字段或更改不透明度,以便文本框在那裏但透明。

+0

我知道文本框可見虛假不是呈現在我的html.But我如何解決這個錯誤? – wotmn

相關問題