在一个函数中使用带有HTML(可选)按钮的JavaScript中的If表达式

在一个函数中使用带有HTML(可选)按钮的JavaScript中的If表达式,javascript,html,radio-button,undefined,nan,Javascript,Html,Radio Button,Undefined,Nan,我正在创建一个HTML表,其中包含必须填写的字段,但我想让其中一个字段成为可选字段。我的代码是这样的: name.html 在if之后的括号之间我应该写什么?你能给我一些想法吗?与NaN比较总是返回false,所以即使NaN==NaN也会返回false。而是使用内置的isNaN功能: if (isNaN(numberTwo)) { 试试IfNumberWO==null | | NumberWO==make you a fiddle:。另外,不要使用表格来布局表单。谢谢,它与+parseFlo

我正在创建一个HTML表,其中包含必须填写的字段,但我想让其中一个字段成为可选字段。我的代码是这样的:

name.html

在if之后的括号之间我应该写什么?你能给我一些想法吗?

与NaN比较总是返回false,所以即使NaN==NaN也会返回false。而是使用内置的isNaN功能:

if (isNaN(numberTwo)) { 

试试IfNumberWO==null | | NumberWO==make you a fiddle:。另外,不要使用表格来布局表单。谢谢,它与+parseFloat一起工作,不是在if循环之前编写的,而是在else循环中编写的:最好与===进行比较。IsNaN应仅用于预期为数字的值。因此不是数字。与空字符串比较将无法正确处理非空、非数字输入。你是对的,IsNan应该只用于预期为数字的值,但我想强调的是,与NaN相比,他出现问题的原因是什么,并给出一种替代方法,但方法正确。
    function forfun() {
    var numberOne=document.getElementById("numb1").value;
    numberOne =parseFloat(numberOne); // numberOne is now really a number
    var numberTwo=document.getElementById("numb2").value;
    numberTwo=parseFloat(numberTwo);
        ...


    if(numberTwo==NaN) { //Here comes the problem, I tried also with 
    //undefined and ==0, but after pressing the button, I become the results
    //of my functions in this loop with result NaN

    numberTwo=2; 
    //and so on
        ...
    }
    }else {

    //After I fill the second field, everything here is ok 
    }
if (isNaN(numberTwo)) {