2016-08-04 25 views
-2

您好我想知道如何計算使用jQuery或Javascript的輸入或文本框中的期數。 例如,我在文本框中的值是「120.0.0」。計數期間「。」在輸入或使用jQuery或JavaScript的文本框

+2

我們不會爲你做你的功課。試一試。如果你無法使用它,請發佈你已經完成的工作以及你遇到的問題,我們會盡力幫助你。 –

+2

可能重複http://stackoverflow.com/questions/881085/count-the-number-of-occurences-of-a-character-in-a-string-in-javascript –

回答

2

使用此聲明:

var length = ("120.0.0.".match(/\./g)).length 

您可以用您的輸入字段值替換120.0.0

1

正則表達式

var temp = "127.0.0.1"; 
 
var count = (temp.match(/[.]/g) || []).length; 
 
console.log(count);

1

使用val()功能然後循環thr如果該字符是句點,則字符串中的每個字符都會遞增計數。

var textBoxVal = $('#myTextBox').val(); 

int periodCount = 0; 
for (var i = 0; i < textBoxVal.length; i++) { 
    if (textBoxVal[i] === '.') { 
     periodCount++; 
    } 
} 
1

$('#btn').click(function() { 
 
    var temp = $('#test').val(); 
 
    var count = (temp.match(/\./g) || []).length; 
 
    console.log(count); 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input id='test' placeholder='Enter the text here.'/> 
 
<button id='btn'>Check</button>

相關問題