鍍金池/ 問(wèn)答/HTML/ 如何獲取select得值判斷第一個(gè)字母是不是數(shù)字

如何獲取select得值判斷第一個(gè)字母是不是數(shù)字

如題,謝謝大家

回答
編輯回答
涼汐

HTML

<select id='test'>
    <option>hello boy</option>
    <option>9ello boy</option>
</select>

JS

var s = document.getElementById('test');
var v = s.value;
// 檢查的正則表達(dá)式
var reg = /^\d/;

if (reg.test(v)) {
    console.log("第一個(gè)字符是數(shù)字!");
} else {
    console.log("第一個(gè)字符不是數(shù)字!");
}
2017年1月16日 11:04
編輯回答
忠妾

var selectedOptions = document.getElementsByTagName("select")[0].options;
var selectedOptionValue = selectedOptions[selectedOptions.selectedIndex].value;
var inNumber = /^\d/.test(selectedOptionValue);
console.log(inNumber)

2017年12月26日 15:41
編輯回答
愛(ài)礙唉

用字符串截取,判斷即可。

2017年4月14日 20:26
編輯回答
久舊酒
    var reg = /^\d.*$/;
    var str = "111aaa";  //這個(gè)就是文本內(nèi)容 
    if(reg.test(str)){
        alert('開(kāi)頭是數(shù)字')  //彈出字段
    }
2018年1月6日 04:37
編輯回答
情已空
<select id='test'>
    <option>hello boy</option>
    <option>9ello boy</option>
</select>

return (/^\d/).test($('#test').val())
2017年1月11日 00:17
編輯回答
維他命
/^\d/.test('4sdf');

這個(gè)意思?

2018年8月2日 16:19
編輯回答
撿肥皂
var isNum=isNaN($("#select").val().substring(0,1));
console.info(isNum);
2017年9月9日 06:26