我有一个javascript字符串到数字转换的问题 [英] I have a problem with a javascript string to number conversion

查看:84
本文介绍了我有一个javascript字符串到数字转换的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试日期,我发现日期类型输入的值

是字符串。我想将返回的字符串的数字部分转换为数字。

我想描述我想要做的最好的方法是粘贴代码。



这里是相关的HTML代码



< input id =dateinputtype =日期> < /输入> 
< input type =buttononclick =calcdiff()value =submit> < / input>





这是我的javascript代码。 ctc元素是我测试的地方,看看我是否得到了正确的输出。



 function calcdiff(){
var datetest = 。的document.getElementById( dateinput)值;
var yearString = datetest.slice(0,4);
var monthString = datetest.slice(5,7);
var dayString = datetest.slice(8,10);
var yearNum = Number(yearString);
var monthNum = Number(monthString);
var dayNum = Number(dayString);
document.getElementById(ctc)。innerHTML = yearNum;





我的尝试:



当我使用typeof yearNum时,我可以看到它被转换为数字类型。但是,当我在上面的代码中将innerHTML设置为yearNum时,它返回NaN。

即使我将ctc元素设置为显示yearString,它显示2018,如果那是我从日期输入元素中选择的年份。



代码有什么问题,因为它返回NaN并且不是数字?

解决方案

正确的语法应该是



  var  yearNum =  Number (yearString); 
var monthNum = Number (monthString);
var dayNum = Number (dayString);





而不是Number(yearString);因为它试图将字符串yearString转换为数字。非常小的更改,传入yearString变量。



顺便说一下,那些输入元素关闭标签看起来有点奇怪。它应该是

 <  输入    id   =  dateinput    type   =  date  /  >  
< 输入 type = button < span class =code-attribute> onclick = calcdiff() value = 提交 / >


I am experimenting with dates and i found out the values from a date type input
are strings. I want to convert the number parts of the string that is returned, to numbers.
I suppose the best way to describe what i am trying to do is by pasting in the code.

here is the relevant HTML code

<input id="dateinput" type="date"> </input>
<input type="button" onclick="calcdiff()" value="submit"> </input>



Here is my javascript code. the ctc element is where i test to see if i get the correct output.

function calcdiff() {
var datetest = document.getElementById("dateinput").value;
  var yearString = datetest.slice(0,4);
  var monthString = datetest.slice(5,7);
  var dayString = datetest.slice(8,10);
var yearNum = Number("yearString");
var monthNum = Number("monthString");
var dayNum = Number("dayString");
document.getElementById("ctc").innerHTML = yearNum;



What I have tried:

when i use typeof yearNum, i can see it is converted to a number type. However,
when i just set the innerHTML to yearNum as in the code above, it returns "NaN".
That is even though if i set the ctc element to display yearString, it displays "2018", if that is the year i choose from the date input element.

What is wrong with the code since it returns "NaN" and not the number?

解决方案

The correct syntax should be

var yearNum = Number(yearString);
  var monthNum = Number(monthString);
  var dayNum = Number(dayString);



and not Number("yearString"); because it try to convert the string "yearString" into number. Very minor changes, pass in the yearString variable.

By the way, those input elements closing tag look a little odd. it should be

<input id="dateinput" type="date"/>
<input type="button" onclick="calcdiff()" value="submit"/>


这篇关于我有一个javascript字符串到数字转换的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆