当用户输入输入时,我的textarea可以计算字符串/字符。这是我的jsfiddle
我的问题是我希望设置条件意味着当字符串/字符是158以上时,我希望在<div id="message">We will deduct 2 credit from your account</div>
内显示消息,并且当字符串/字符是316以上时消息将更新并显示&lt; div id="message">We will deduct 3 credit from your account</div>
并继续..
示例:
158 *2
158 = We will deduct 2 credit from your account
316 = We will deduct 3 credit from your account
474 = We will deduct 4 credit from your account
632 = We will deduct 5 credit from your account
...
答案 0 :(得分:2)
试试吧
$('#myInput').keyup(function() {
$('#charCount').text(this.value.length);
var c = parseInt(this.value.length / 158);
if(c > 0)
$('#message').text('We will deduct '+c+' credit from your account');
});
答案 1 :(得分:0)
1)找到String的长度
2)除以158
3)在答案中添加“1”
4)使用字符串连接创建消息 - “我们将从您的帐户中扣除”+(3)+“信用额”的输出
5)jQuery(“#message”)。html(“我们将从您的帐户中扣除”+(3)+“信用额的输出”);
答案 2 :(得分:0)
试试这个
$('#myInput').keyup(function() {
var len = this.value.length ;
$('#charCount').text(len);
var creditCount = 158;
if( len > creditCount ){
var credits = Math.floor(len / creditCount );
$('#message').html('We will deduct '+ credits + ' credit from your account;' )
}
});
<强> Check DEMO 强>
答案 3 :(得分:0)
在textarea keyup上你可以查看否。字符存在,因此您可以编写if-else条件来显示消息。
检查一下:working fiddle
$('#myInput').keyup(function() {
if(this.value.length<=158)
$('#charCount').text('We will deduct 1 credit from your account');
else if(this.value.length>158 || this.value.length<316)
$('#charCount').text('We will deduct 2 credit from your account');
else if(this.value.length>316|| this.value.length<316)
$('#charCount').text('We will deduct 3 credit from your account');
});
答案 4 :(得分:0)
$('#myInput').keyup(function() {
$('#charCount').text(this.value.length);
var length = this.value.length;
if (length % 158 == 0) {
$('#message').html('We will deduct '+ (length/158) +
' credit from your account;' )
}
});