[英]Uncaught TypeError: Cannot read property 'value' of null
Hello I am working on an ASP.NET page where I want to view data from database using AJAX您好,我正在处理一个 ASP.NET 页面,我想在其中使用 AJAX 查看数据库中的数据
What I have done is created the following C# method:我所做的是创建了以下 C# 方法:
[System.Web.Services.WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]
public static void ViewData(string contNumber, string BookingNo)
{
if (contNumber != "" && BookingNo != "")
{
string container = contNumber;
string booking = BookingNo;
db.SelectData(container, booking);
HttpContext.Current.Response.Write("IN!!"+ booking);
}
}
I then call this method using AJAX, here the code:然后我使用 AJAX 调用此方法,代码如下:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script
function View() {
var container = document.getElementById("#<%=contNoTxtBox.ClientID%>").value;
var booking = document.getElementById("#<%=bookingNoTxtBox.ClientID%>").value;
// var cont = container.value;
// var book = booking.value;
$.ajax(
{
type: "POST",
url: "VGMForm.aspx/ViewData",
data: '{contNumber: "' + $(container) + '", BookingNo: "' + $(booking) + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json"
});
}
From my HTML page I call the function using the following code:在我的 HTML 页面中,我使用以下代码调用该函数:
<input id="Button1" type="button" value="button" onclick="View()" />
However, when I click on the button, I receive the following error:但是,当我单击按钮时,收到以下错误:
Uncaught TypeError: Cannot read property 'value' of null
未捕获的类型错误:无法读取 null 的属性“值”
I am unsure as to what is causing this error, what is the cause and how can I resolve it?我不确定导致此错误的原因是什么,原因是什么以及如何解决?
Some of getElementById
returns null
一些
getElementById
返回null
document.getElementById("#<%=contNoTxtBox.ClientID%>")
or或者
document.getElementById("#<%=bookingNoTxtBox.ClientID%>")
I was able to solve it by removing the .ClientID from the script as the following:我能够通过从脚本中删除 .ClientID 来解决它,如下所示:
var container = document.getElementById("contNoTxtBox").value;
var booking = document.getElementById("bookingNoTxtBox").value;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.