Javascript 使用Visual Studio在ASP.NET上未定义jQuery和val()
我尝试使用此代码使用jQuery获取var值。由于某种原因,name等于null或未定义,我似乎无法理解它Javascript 使用Visual Studio在ASP.NET上未定义jQuery和val(),javascript,jquery,asp.net,visual-studio-2010,Javascript,Jquery,Asp.net,Visual Studio 2010,我尝试使用此代码使用jQuery获取var值。由于某种原因,name等于null或未定义,我似乎无法理解它 <asp:TextBox ID="Text_Email" runat="server" CssClass="" Width="234px">Email</asp:TextBox> var name = $("#<%=Text_Email.ClientID%>").value; // name == undefin
<asp:TextBox
ID="Text_Email"
runat="server"
CssClass=""
Width="234px">Email</asp:TextBox>
var name = $("#<%=Text_Email.ClientID%>").value; // name == undefined
电子邮件
变量名称=$(“#”)值;//name==未定义
以下是我的主要剧本:
$(document).ready(function () {
$("#<%=send_info.ClientID%>").click(function () {
// var name = document.getElementById("#<%=Text_Name.ClientID%>").value;
var name = $("#<%=Text_Email.ClientID%>").value;
var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
if (reg.test(name) == false) {
$(this).val("");
$("#error_email_adress").removeClass('email_valid');
$("#error_email_adress").addClass('email_invalid');
return false;
} else {
$("#error_email_adress").removeClass('email_invalid');
$("#error_email_adress").addClass('email_valid');
alert("Message sent");
return true;
}
});
});
$(文档).ready(函数(){
$(“#”)单击(函数(){
//var name=document.getElementById(“#”)value;
变量名称=$(“#”)值;
变量reg=/^([A-Za-z0-9\-\.])+\@([A-Za-z0-9\-\.])+\.([A-Za-z]{2,4})$/;
如果(注册测试(名称)=错误){
$(此).val(“”);
$(“#error#u email_address”).removeClass('email_valid');
$(“#错误#电子邮件地址”).addClass('email#invalid');
返回false;
}否则{
$(“#error#u email_address”).removeClass(“email_invalid”);
$(“#error#u email_address”).addClass(“email_valid”);
警报(“已发送消息”);
返回true;
}
});
});
这一部分也让我感到困惑。为什么会发生这种情况?有人能给我解释一下为什么这里定义了这个名字吗
$("#<%=Text_Email.ClientID%>").click(function () {
var name = this.value;//name != undefined;
});
$(“#”)点击(函数(){
var name=this.value;//name!=未定义;
});
而这里的名称是未定义的
var name = $("#<%=Text_Email.ClientID%>").value; // name == undefined;
var name=$(“#”).value;//名称==未定义;
谢谢您的帮助。jQuery没有值,但有用于textbox的val()函数 试试这个
var name = $("#<%=Text_Email.ClientID%>").val();
var name=$(“#”)val();
您的代码将被删除
$("#<%=Text_Email.ClientID%>").click(function () {
var name = $(this).val();// you will get value in name by this statement.
});
$(“#”)点击(函数(){
var name=$(this).val();//您将通过此语句获得name中的值。
});
jQuery没有值,但有用于textbox的val()函数
试试这个
var name = $("#<%=Text_Email.ClientID%>").val();
var name=$(“#”)val();
您的代码将被删除
$("#<%=Text_Email.ClientID%>").click(function () {
var name = $(this).val();// you will get value in name by this statement.
});
$(“#”)点击(函数(){
var name=$(this).val();//您将通过此语句获得name中的值。
});
试试这个,而不是你写的
$(this).val()
请注意,jquery的语法几乎是直截了当的。有关元素值的函数是$.val(),您可以通过传递字符串来设置元素的值,如下所示:
$(this).val("the desired value");
或者可以通过不传递任何内容来获取元素的值 试试这个,而不是你写的
$(this).val()
$("#<%=Text_Email.ClientID%>").click(function () {
var name = this.value; //<-- 'this' here is a DOM HTML Object which has .value property
var elem = jQuery(this); //<-- converts the DOM Object into a jQuery object, jQuery does not have .value property
var nameVal = elem.val(); //<-- get the value the jQuery way
});
请注意,jquery的语法几乎是直截了当的。有关元素值的函数是$.val(),您可以通过传递字符串来设置元素的值,如下所示:
$(this).val("the desired value");
或者可以通过不传递任何内容来获取元素的值 $(“#”)点击(函数(){
$("#<%=Text_Email.ClientID%>").click(function () {
var name = this.value; //<-- 'this' here is a DOM HTML Object which has .value property
var elem = jQuery(this); //<-- converts the DOM Object into a jQuery object, jQuery does not have .value property
var nameVal = elem.val(); //<-- get the value the jQuery way
});
var name=this.value;//$(“#”)。单击(函数(){
var name=this.value;//我尝试了很长时间的加载,结果没有通过下一行。更正并更新了答案epascarello,感谢您指出它。var name=$(“#”).val();由于某些原因,jquery无法移动下一行我尝试了很长的加载,结果没有通过下一行。更正并更新了答案epascarello,感谢您指出它。var name=$(“#”).val();由于某些原因,jquery无法移动下一行