我有一个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>
当前的JQuery
//Disables/Enables submit button on modal
$("#MainContent_fldPlace").on("blur", function ()
{
disableButton();
});
$("#MainContent_fldLocation").on("blur", function ()
{
disableButton();
});
$("#MainContent_fldName").on("blur", function ()
{
disableButton();
});
function disableButton()
{
if ($('#MainContent_fldPlace').val() != '' && $('#MainContent_fldLocation').val() != '' && $('#MainContent_fldName').val() != '')
{
$("#MainContent_btnSubmitNewPlace").prop('disabled', false);
}
else
{
$("#MainContent_btnSubmitNewPlace").prop('disabled', true);
}
}
////////
我尝试添加:
$('#MainContent_placeExists').visible == true
到我的IF
,但它不起作用
下面是我在服务器端显示/隐藏标签的代码
#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>
答案 0 :(得分:1)
这里有一种误解。 ASP .NEP Visible=false
控件将不会呈现,因此您无法找到。 visible
不是html属性。
您可以使用jquery
length
function disableButton(){
if($('#<%= placeExists.ClientID %>').length === 0){
$("#MainContent_btnSubmitNewPlace").prop('disabled', false);
}
else {
$("#MainContent_btnSubmitNewPlace").prop('disabled', true);
}
}
答案 1 :(得分:1)
要成为标签,您需要使用#LabelID.text()或#labelID.html()
if($("#MainContent_placeExists").text() != '')
{
// Do your code
}
or you can do as @LucasSouza said
if($("#MainContent_placeExists").length > 0)
{
// Do your code
}