简体   繁体   English

通过ID搜索文本区域并突出显示-Javascript

[英]Search textarea by id and highlight - Javascript

I have multiple textarea on my page as below: 我的页面上有多个文本区域,如下所示:

<textarea rows="1" id="textid1"></textarea>
<textarea rows="1" id="textid2"></textarea>

and so on 等等

On button click I am validating the input that was entered in each textbox. 在按钮上单击,我正在验证在每个文本框中输入的输入。 What I am looking for is how to look for "textid1" if it has errors and then highlight it. 我正在寻找的是如何在出现错误的情况下查找“ textid1”,然后突出显示它。

Currently what I had was a single placeholder for error that gives a generic error of all the texbox right at the bottom. 目前我只有一个错误占位符,它在底部给出了所有texbox的一般错误。 But what I want is to highlight the texbox that has error and show the error text right below it. 但是我想要突出显示有错误的texbox,并在其下方显示错误文本。

Thanks 谢谢

Since you have unique ID, you can do a Id selector query search with jQuery 由于您具有唯一的ID,因此可以使用jQuery执行ID选择器查询搜索

<script>
var myText = $( "#textid1" ).text();
...validate your text
</script>

You should have a <div> or place holder right below the text and displace your validation. 您应该在文本下方放置一个<div>或占位符,并替换您的验证。

You can also use regex to validate your text...I am not sure what is the error and or how are you defining error in your text. 您也可以使用正则表达式来验证您的文本...我不确定是什么错误以及如何在文本中定义错误。

You can send your textarea's id with input's data, if an error accrued, return the textarea's id with your error message and find the textarea like this: 您可以将textarea's id与输入的数据一起发送,如果发生错误,则返回包含错误消息的textarea's id ,并找到如下所示的textarea:

<script> 
var myTextarea = $("#returnedId");
</script>

And do whatever you want with finded textarea 并使用找到的textarea做任何您想做的事

You can do it like this: (for the demo I made it so the first has to be filled and the second empty) 您可以这样操作:(对于演示,我做到了,因此第一个必须填充,第二个为空)

 $("button").on("click", function() { let b = $("#textid1"); if (!b.val()) //here check for the error you are looking for b.addClass("error") else b.removeClass("error") }); $("button").on("click", function() { let b = $("#textid2"); if (b.val()) //here check for the error you are looking for b.addClass("error") else b.removeClass("error") }); 
 .error { border: solid 1px red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <textarea rows="1" id="textid1"></textarea> <textarea rows="1" id="textid2"></textarea> <button>Check</button> 

If you want to check for the same thing on multiple inputs you can use each : 如果要在多个输入上检查相同的内容,则可以使用each输入:

 $("button").on("click", function() { $("textarea").each((a, b) => { if (!$(b).val()) //here check for the error you are looking for $(b).addClass("error") else $(b).removeClass("error") }); }); 
 .error { border: solid 1px red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <textarea rows="1" id="textid1"></textarea> <textarea rows="1" id="textid2"></textarea> <button>Check</button> 

ID Selector (“#id”) ID选择器(“ #id”)

For id selectors, jQuery uses the JavaScript function document.getElementById() , which is extremely efficient. 对于id选择器,jQuery使用JavaScript函数document.getElementById() ,它非常高效。 When another selector is attached to the id selector, such as h2#pageTitle , jQuery performs an additional check before identifying the element as a match. 当另一个选择器附加到id选择器(例如h2#pageTitle ,jQuery在将元素标识为匹配项之前执行附加检查。

Effect 影响

Apply an animation effect to an element. 将动画效果应用于元素。

 $('button').click(function() { //After validations $('#mytext').effect( 'highlight', {}, 500 ); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <textarea id="mytext" class="animated">Hello World</textarea><br> <button> Click me! </button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM