[英]How to Get an ID Attribute for a text-area that has been replaced by ckeditor
我正在嘗試使用(JQuery)已被ckeditor替換的文本區域進行驗證,似乎無法獲取我的文本區域。
here<div class="form-group">
@Html.Label("Instructions", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextArea("Instruction", new { @class = "form-control question-textarea", @style = "height:100px", @placeholder = "Enter instructions here" })
Instructions: <span id="linesUsed">0</span> of 15
</div>
</div>
我的JavaScript和Jquery
enter code here <script>
CKEDITOR.replace("Instruction");
</script>
<script>
$(document).ready(function () {
var lines = 15;
var linesUsed = $('#linesUsed');
$('#cke_1_contents').keydown(function (e) {
newLines = $(this).val().split("</p>").length;
linesUsed.text(newLines);
if (e.keyCode == 13 && newLines >= lines) {
linesUsed.css('color', 'red');
return false;
}
else {
linesUsed.css('color', '');
}
});
});
</script>
看來我的Jquery沒有為我的文本區域獲取ID,有人知道我如何在ckeditor中獲取文本區域ID。
嘗試使用事件委托,您可以在這里閱讀
“事件委托使我們可以將單個事件偵聽器附加到父元素,這將為與選擇器匹配的所有后代觸發,無論這些后代現在存在還是將來添加。”
更改$('#cke_1_contents').keydown(function (e) {
到$(document).on('keydown', '#cke_1_contents', function(e){} )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.