Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
156 views
in Technique[技术] by (71.8m points)

javascript - How to change maxlength attributes for all classes of "field answer"

I have a class named "field answer" which has an attribute "maxlength" of 250. I want to change this to 9999 for each occurrence of this maxlength attribute.

<div class="field answer">
   <textarea name="ctl00$Main$ctrl_16161" rows="2" cols="20" id="ctl00_Main_ctrl_16161" class="long" 
    onkeyup="return cw.Utilities.trimLength(this)" maxlength="250" style="resize: none; height: 61px; 
    overflow-y: hidden;"></textarea>
</div>

I am a total noob when it comes to this so please understand. I've tried the following but no luck.

var fieldAnswer = document.getElementsByClassName("field answer");
if (fieldAnswer.namedItem = "maxlength") {
  fieldAnswer.maxlength = 9999;

The only thing that seems to work is when I call each tag individually like the following:

$(function() {
$("#ctl00_Main_ctrl_16161").attr("maxlength",9999);});
question from:https://stackoverflow.com/questions/65905244/how-to-change-maxlength-attributes-for-all-classes-of-field-answer

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The maxlength attribute is on the textarea, not the div.field element. In addition note that your jQuery is using a selector which is generated by ASP.Net and may change. As such you can achieve what you require most simply by selecting the textarea elements as a child of the parent div.field and updating all of them at once:

jQuery($ => {
  $('div.field.answer textarea').prop('maxlength', 9999);
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...