2017-09-26 73 views
0

我添加了兩個文本框,即CompanyName和FilePrefix,因此我必須將divhtml設置爲null,並使用按鍵事件清除錯誤消息。所以,如果用戶在文本框上鍵入任何內容,那麼應該清除錯誤消息。如何使用jquery中的keypress事件刪除錯誤類?

在後面的代碼中,我使用了這段代碼。

divMsg.InnerHtml = "Please enter the CompanyName and FilePrefixName"; 
    divMsg.Attributes.Add("Class", "alert alert-danger alert-dismissible"); 

這裏是我的asp.net代碼:

<div class="box-body"> 
    <div class="row"> 
    <div id="divMsg" runat="server"></div> 

    </div> 
    <div class="form-group"> 
    <label for="exampleInputCompanyName">CompanyName</label> 
    <asp:TextBox ID="txtCompanyName" class="form-control" runat="server" placeholder="CompanyName"></asp:TextBox> 
    </div> 
    <div class="form-group"> 
    <label for="exampleInputFile-Prefix">File-Prefix</label> 
    <asp:TextBox ID="txtFilePrefix" class="form-control" runat="server" placeholder="File-Prefix"></asp:TextBox> 
    </div> 
</div> 

和jQuery

$(document).keypress(function(e) { 
    $(this).removeClass("divMsg"); 
}); 
+0

使用。對( 「輸入」,... – mplungjan

+0

嘗試清空使用 $( 「#divMsg」)HTML( '')的內容; – aswindev

回答

0

您已在後端添加了「alert alert-danger alert-dismissible」類。所以,你需要在按鍵上刪除這個類。

$(document).on("keypress", "input[type='text']", function(e) { 
    $(this).removeClass("alert alert-danger alert-dismissible"); 
}); 

否則您可以使html空白。

$(document).on("keypress", "input[type='text']", function(e) { 
    $("#divMsg").html(""); 
}); 
+1

謝謝你disha.Its工作正常。 – Sridhar

0
$('input[type=text]').on('input',function (e) { 
    if($('#txtCompanyName').val() !== '' && $('#txtFilePrefix').val() !== '') 
    { 
     $('#divMsg').text(""); 
    } 
}); 

因爲如果你將刪除#divMsg ID本身則無法顯示如果用戶從任一文本框中刪除值,則返回錯誤。正如我在代碼中顯示的那樣,只需從div中刪除文本即可。

+0

@Sridhar,希望這將有助於你。 –

1

此:

$(document).keypress(function(e) { 

文檔是否在尋找一個按鍵任何地方,試圖從文檔對象本身刪除類。相反,你想在你的input元素上做到這一點。事情是這樣的:

$(document).on('keypress', 'input[type=text]', function(e) { 

這仍然結合在文檔級別的單一事件處理程序,但隻影響input type="text"元素。以及專門提到this的元素。

+0

我更喜歡.on(「輸入」我自己 – mplungjan