admin管理员组文章数量:1291128
I have something that I am trying to implement in react, its a simple functionality that calculates how many characters have been typed in to a textarea.
here goes the source code
var WordCount = React.createClass({
getInitialState: function() {
return{ contacts: [], fullName: "What Ever", smsBalance: 0, mand: 'Send Now', charsPerPage: 160, pageCount:0 };
},
wordCount: function(e){
var currentText = e.target.value;
//Now we need to recalculate the number of characters that have been typed in so far
var characterCount = currentText.length;
var charsPerPageCount = this.state.charsPerPage;
var unitCount = Math.round(characterCount/charsPerPageCount);
this.setState({pageCount: unitCount});
},
render: function() {
return(
<div className="md-card">
<div className="user_content">
<ul className="uk-margin">
<div className="uk-margin-top">
<div className="uk-grid" data-uk-grid-margin="">
<div className="uk-width-medium-1-1">
<div className="md-input-wrapper">
<label htmlFor="Message">And Now Your Message</label>
<textarea className="md-input autosize_init" cols="30"
data-val="true"
data-val-required="The Message field is required."
id="Message" ref="Message"
rows="3" onChange={this.wordcount} style={{overflowX: "hidden", wordWrap: "break-word", height: 97+"PX"}}></textarea>
<span className="md-input-bar"></span></div>
<br/>
<span className="md-color-grey-300">
Current cost {this.state.pageCount}
</span>
</div>
</div>
</div>
</ul>
</div>
</div>
);
}
});
ReactDOM.render(<WordCount/>, document.getElementById("PageContent"))
Ideally what i intend to achieve is count the number of characters that the user has entered so far in the text area and divide that number by some preset value to get the number of pages and display the number of pages for the users entry to the user
However, the sate variable 'pageCount' remains at zero perpetually. Please What am I doing wrong
Regards Peter
I have something that I am trying to implement in react, its a simple functionality that calculates how many characters have been typed in to a textarea.
here goes the source code
var WordCount = React.createClass({
getInitialState: function() {
return{ contacts: [], fullName: "What Ever", smsBalance: 0, mand: 'Send Now', charsPerPage: 160, pageCount:0 };
},
wordCount: function(e){
var currentText = e.target.value;
//Now we need to recalculate the number of characters that have been typed in so far
var characterCount = currentText.length;
var charsPerPageCount = this.state.charsPerPage;
var unitCount = Math.round(characterCount/charsPerPageCount);
this.setState({pageCount: unitCount});
},
render: function() {
return(
<div className="md-card">
<div className="user_content">
<ul className="uk-margin">
<div className="uk-margin-top">
<div className="uk-grid" data-uk-grid-margin="">
<div className="uk-width-medium-1-1">
<div className="md-input-wrapper">
<label htmlFor="Message">And Now Your Message</label>
<textarea className="md-input autosize_init" cols="30"
data-val="true"
data-val-required="The Message field is required."
id="Message" ref="Message"
rows="3" onChange={this.wordcount} style={{overflowX: "hidden", wordWrap: "break-word", height: 97+"PX"}}></textarea>
<span className="md-input-bar"></span></div>
<br/>
<span className="md-color-grey-300">
Current cost {this.state.pageCount}
</span>
</div>
</div>
</div>
</ul>
</div>
</div>
);
}
});
ReactDOM.render(<WordCount/>, document.getElementById("PageContent"))
Ideally what i intend to achieve is count the number of characters that the user has entered so far in the text area and divide that number by some preset value to get the number of pages and display the number of pages for the users entry to the user
However, the sate variable 'pageCount' remains at zero perpetually. Please What am I doing wrong
Regards Peter
Share Improve this question asked Jan 19, 2016 at 10:07 Paul PlatoPaul Plato 1,5016 gold badges28 silver badges37 bronze badges2 Answers
Reset to default 7A silly mistake, change onChange={this.wordcount}
to onChange={this.wordCount}
.
var yourText = this.state.yourTextArea;
yourText.split("\n").length
本文标签: javascriptCounting characters from TextArea in ReactJsStack Overflow
版权声明:本文标题:javascript - Counting characters from TextArea in ReactJs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741516170a2382901.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论