【发布时间】:2018-06-12 04:15:41
【问题描述】:
我网站的输入框是这样的:(click here to view image)
jsx 文件中的代码如下:
render: function () {
return (
<form id="addcourse" role="form" className="form-horizontal" enctype="multipart/form-data" ref="courseForm">
<fieldset>
<CategoryLoader callback={this.handleCategoryChange}/>
<div className="form-group">
<label className="control-label col-md-2 col-md-offset-1">Title</label>
<div className="col-md-7 col-md-offset-1">
<input name="title" type="text" maxLength="6" id="abcd" className="form-control" ref="title" placeholder="Title (Maximum 100 characters)"/>
</div>
</div>
<div className="form-group">
<label className="control-label col-md-2 col-md-offset-1">Course Image</label>
<div className="col-md-7 col-md-offset-1">
<input name="image" type="file" className="form-control" ref="image"/>
<p className="help-block">Upload image for the course </p>
</div>
</div>
<div className="form-group">
<label className="control-label col-md-2 col-md-offset-1">Enrollment Type</label>
<div className="col-md-7 col-md-offset-1">
<select ref="enrollment_type" name="enrollment_type" className="form-control">
<option value="O">Open</option>
<option value="M">Moderated</option>
</select>
</div>
</div>
<div className="form-group">
<div className="col-md-2 col-md-offset-2 addcoursebutton">
<button ref="submit" className="btn btn-primary" type="button" onClick={this.add_course}>Add
Course
</button>
</div>
<div className="col-md-1">
<button type="button" onClick={this.props.closeCallBack} className="btn btn-danger">
Close
</button>
</div>
</div>
</fieldset>
</form>
);
我试过 maxLength="6"。
输入时不会超过 6 个字符,但不会出现“最大长度为 6 个字符”之类的错误。
所以,当用户开始在该字段中输入时,我希望它在输入字段下显示一条消息,“最多允许 6 个字符”。
我该怎么做?
【问题讨论】:
-
您的“问题”只是说明您的要求,而不是任何具体问题。您是否只想在他们尝试输入更多内容时显示消息?如果是这样,您可以向
<input/>添加一个onKeyPress处理程序,该处理程序在他们尝试键入内容时检查长度,并添加逻辑以在输入值已经是6 个字符时显示错误。可能只是一个简单的布尔状态,然后有条件地渲染一个带有所需“最大字符=6”错误消息的元素。这只是一个例子。 -
你在项目中使用 jQuery 吗?
标签: javascript reactjs maxlength