亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

請問這里判斷數字的返回值,為什么只能是數字?

請問這里判斷數字的返回值,為什么只能是數字?

依韻S1 2016-12-19 09:33:50
.p1?{?background-color:#abcdef;?color:green;?} .p2?{?background-color:#000;?color:#fff;?} div?{?margin:10px;?}<input?type="text"?class="js-text2"?value=""/> <input?type="text"?class="js-text3"?value=""/> <input?type="button"?onclick="calculate('+')"?value="加"?/> <input?type="button"?onclick="calculate('-')"?value="減"?/> <input?type="button"?onclick="calculate('*')"?value="乘"?/> <input?type="button"?onclick="calculate('/')"?value="除"?/> <script> function?calculate(a){ var?num1?=?$("js-text2").value; var?num2?=?$('js-text3').value; var?isnum1?=?isnumber(num1); var?isnum2?=?isnumber(num2); if(isnum1==2||isnum2==2){ alert("含有非法字符"); return?false; }else{ if(?isnum1==0?&&?isnum2==0?)?{ if(?a=='+'?)?{ alert(parseInt(num1)+parseInt(num2)); } else?if(?a=='-'?)?{ alert(parseInt(num1)-parseInt(num2)); } else?if(?a=='*'?)?{ alert(parseInt(num1)*parseInt(num2)); } else?if(?a=='/'?)?{ alert(parseInt(num1)/parseInt(num2)); } } } } function?$(classname){ return?document.getElementsByClassName(classname)[0]; } /* return?0:整數;?1:小數;?2:非法字符 */ //只能整數運算 function?isnumber(a){ if(!isNaN(a)){ var?array?=?a.split('.'); var?count?=?array.length; if(count==1){ return?0; }else{ if(count==2){ if(parseInt(array[1])==0){ return?0; }else{ return?2; } }else{ return?2; } } }else{ return?2; } } </script>這是一個整數的加減乘除,我想問的是判斷是否為整數(函數 isnumber)返回的值,為什么只能是數字。我嘗試將 返回的數字改成字母就會報錯
查看完整描述

2 回答

?
這是一首

TA貢獻55條經驗 獲得超6個贊

function?isNum(s)?{
?//var?regu?=?"^([0-9]*)$";
?var?regu?=?"^([0-9]*[.0-9])$";?//?小數測試
?var?re?=?new?RegExp(regu);
?if?(s.search(re)?!=?-1)
??return?true;
?else
??return?false;
}

試試這個

查看完整回答
反對 回復 2016-12-19
  • 依韻S1
    依韻S1
    兄弟我只想知道為啥我上面那段只能是數字,,
  • 依韻S1
    依韻S1
    正則我還沒接觸到。就算復制了你的代碼我也不懂什么意思呢。。
  • 2 回答
  • 0 關注
  • 1364 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號