Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net使用JQuery检查标签是否显示_Jquery_Asp.net - Fatal编程技术网

Asp.net使用JQuery检查标签是否显示

Asp.net使用JQuery检查标签是否显示,jquery,asp.net,Jquery,Asp.net,我有一个Asp.net应用程序,我有一个JQuery功能,可以禁用/启用我的“提交”按钮。目前,如果我的任何字段为空,我的“提交”按钮将被禁用,但我现在想向其添加另一个条件,以查看是否显示我的错误消息 “我的消息”显示在服务器端,因此,如果该名称已存在,则在您签出该字段时会立即显示一条错误消息(所有这些都可以正常工作) 我根本无法让它工作 标签的HTML格式 <asp:Label id="placeExists" runat="server" Text="The place you sug

我有一个
Asp.net
应用程序,我有一个
JQuery
功能,可以禁用/启用我的“提交”按钮。目前,如果我的任何字段为空,我的“提交”按钮将被禁用,但我现在想向其添加另一个条件,以查看是否显示我的错误消息

“我的消息”显示在服务器端,因此,如果该名称已存在,则在您签出该字段时会立即显示一条错误消息(所有这些都可以正常工作)

我根本无法让它工作

标签的HTML格式

<asp:Label id="placeExists" runat="server" Text="The place you suggested already exists in the list of 'Places'." Visible="false" style="color: red"></asp:Label>
我试着加上:

$('#MainContent_placeExists').visible == true
到我的
如果
但它不起作用

下面是我在服务器端显示/隐藏标签的代码

 #region Checks if the place exists in 'Places' db when field clicked out of
        protected void fldPlace_TextChanged(object sender, EventArgs e)
        {
            //Keeps the 'Suggest A Place' modal open
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "keepOpenSuggestPlaceModal();", true);

            if (!string.IsNullOrEmpty(fldPlace.Text))
            {
                string connection = ConfigurationManager.ConnectionStrings["PaydayLunchConnectionString1"].ConnectionString;
                SqlConnection conn = new SqlConnection(connection);

                conn.Open();
                SqlCommand cmd = new SqlCommand("SELECT 1 FROM Places WHERE Place = @Place", conn);
                cmd.Parameters.AddWithValue("@Place", fldPlace.Text);
                SqlDataReader rd = cmd.ExecuteReader();

                if (rd.HasRows)
                {
                    placeExists.Visible = true;
                }
                else
                {
                    placeExists.Visible = false;
                }
            }
        }
        #endregion
从网站呈现HTML

<div class="modal-body">
     <p>If you would like to suggest a place please populate the fields below and click 'Submit'.</p>
     <p>Your request will then be reviewed by a site administrator and if accepted, it will be added to the list.</p>
     <span id="MainContent_placeExists" style="color: red;">The place you suggested already exists in the list of 'Places'.</span>    
     <div class="col-sm-offset-2">
          <div class="form-group">
               <label class="col-sm-3 control-label" id="MainContent_lblPlace" for="MainContent_fldPlace">Place</label>
               <div class="col-sm-6">
                    <input name="ctl00$MainContent$fldPlace" class="form-control" id="MainContent_fldPlace" onkeypress="if (WebForm_TextBoxKeyHandler(event) == false) return false;" onchange="javascript:setTimeout('__doPostBack(\'ctl00$MainContent$fldPlace\',\'\')', 0)" type="text" value="Blaize">
                </div>
           </div>
           <div class="form-group">
                <label class="col-sm-3 control-label" id="MainContent_lblLocation" for="MainContent_fldLocation">Location</label>
                <div class="col-sm-6">
                     <input name="ctl00$MainContent$fldLocation" class="form-control" id="MainContent_fldLocation" type="text">
                 </div>
           </div>
           <div class="row">
                <label class="col-sm-3 control-label" id="MainContent_lblName" for="MainContent_fldName">Your Name</label>
                <div class="col-sm-6">
                     <input name="ctl00$MainContent$fldName" class="form-control" id="MainContent_fldName" type="text">
                </div>
           </div>
      </div>
 </div>

如果您想推荐一个地点,请填写下面的字段并单击“提交”

您的请求将由站点管理员审核,如果被接受,将被添加到列表中

您建议的地点已存在于“地点”列表中。 放置 位置 你的名字
这里有一个误解。ASP.NEP
Visible=false
控件将不会呈现,因此您无法找到<代码>可见不是html属性。 您可以使用jquery检查元素的
长度

function disableButton(){
   if($('#<%= placeExists.ClientID %>').length === 0){
      $("#MainContent_btnSubmitNewPlace").prop('disabled', false);
   }
   else {
      $("#MainContent_btnSubmitNewPlace").prop('disabled', true);
   }
}
功能禁用按钮(){
如果($('#')。长度===0){
$(“#MainContent_btnSubmitNewPlace”).prop('disabled',false);
}
否则{
$(“#MainContent_btnSubmitNewPlace”).prop('disabled',true);
}
}

要成为标签,您需要使用#LabelID.text()或#LabelID.html()


您可以显示标签的呈现HTML,而不是服务器端控件的HTML。您应该使用like
$('#')。show()
@RoryMcCrossan添加了web呈现的HTML,但添加了所有内容。它将其呈现为
span
Try
$('MainContent\u placeExists')。visible==true
=>
$('MainContent\u placeExists')。css('visibility','visible')
要检查元素是否可见,可以使用
$(选择器).is(':visible')。三个
blur
处理程序可以写成
$(“blur”,disableButton)我已将此添加到我的
IF
中,它似乎工作正常,但方向不对。当信息未显示时,我的“提交”按钮仍处于禁用状态,当它显示时,它将启用我的按钮,但应该是相反的(显示时禁用,未显示时启用,根据我的初始帖子中的my
JQuery
IF
,您第一次提出建议并添加到我现有的
IF
,第一次工作
function disableButton(){
   if($('#<%= placeExists.ClientID %>').length === 0){
      $("#MainContent_btnSubmitNewPlace").prop('disabled', false);
   }
   else {
      $("#MainContent_btnSubmitNewPlace").prop('disabled', true);
   }
}
if($("#MainContent_placeExists").text() != '')
{
   // Do your code 
} 

or you can do as @LucasSouza said  

if($("#MainContent_placeExists").length > 0)

{
   // Do your code 
}