Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/404.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript,将Nan转换为0.00后验证值时出现问题_Javascript_Nan - Fatal编程技术网

Javascript,将Nan转换为0.00后验证值时出现问题

Javascript,将Nan转换为0.00后验证值时出现问题,javascript,nan,Javascript,Nan,最初,我使用下面的javascript来验证权重。但要意识到,可能没有自我定义的网格,这意味着重量将是很小的。然后我添加了一个if/else语句来将NaN转换为0.00。转换正在工作,但重量验证不再工作。请告知如何进行修复,以便将NaN值转换为0.00,并执行重量检查 原件: $j( '#' + config.TableGridNames._02_SelfDefined + ' tbody tr td:nth-child(' + config.SelfDefinedGrid.Edit_W

最初,我使用下面的javascript来验证权重。但要意识到,可能没有自我定义的网格,这意味着重量将是很小的。然后我添加了一个if/else语句来将NaN转换为0.00。转换正在工作,但重量验证不再工作。请告知如何进行修复,以便将NaN值转换为0.00,并执行重量检查

原件:

    $j( '#' + config.TableGridNames._02_SelfDefined + ' tbody tr td:nth-child(' + config.SelfDefinedGrid.Edit_Weight_Idx + ')' ).each( function ()
    {
        var row_index = $j( this ).closest( 'tr' ).index();
        /* skip over the header row; return is equivalent to continue */
        if ( row_index == 0 ) { return; }

        $j( this ).css( { 'background-color': 'white' } );
        var oWeight = $j( this ).find( 'input' );
        if ( !helper.isNumber( oWeight.val() ) || parseFloat( oWeight.val() ) < 0 || parseFloat( oWeight.val() ) > 1 )
        {
            $j( this ).css( { 'background-color': '#FFD1D1' } );
            blIsError = true;
        }

        decWeight = decWeight + parseFloat( oWeight.val() );
        console.log( 'Self-Defined (' + row_index.toString() + ') ' + decWeight.toString() );
    } );
$j('#'+config.TableGridNames._02_SelfDefined+'t正文tr td:n子项('+config.SelfDefinedGrid.Edit_Weight_Idx+'))。每个(函数()
{
var row_index=$j(this).closest('tr').index();
/*跳过标题行;return相当于continue*/
如果(row_index==0){return;}
$j(this.css({'background color':'white'});
var oWeight=$j(this.find('input');
如果(!helper.isNumber(oWeight.val())| | parseFloat(oWeight.val())<0 | | parseFloat(oWeight.val())>1)
{
$j(this.css({'background color':'#FFD1D1'});
blIsError=true;
}
decWeight=decWeight+parseFloat(oWeight.val());
log('Self-Defined('+row_index.toString()+')'+decWeight.toString());
} );
更新:

    $j( '#' + config.TableGridNames._02_SelfDefined + ' tbody tr td:nth-child(' + config.SelfDefinedGrid.Edit_Weight_Idx + ')' ).each( function ()
    {
        var row_index = $j( this ).closest( 'tr' ).index();
        /* skip over the header row; return is equivalent to continue */
        if ( row_index == 0 ) { return; }

        $j( this ).css( { 'background-color': 'white' } );
        var oWeight = $j( this ).find( 'input' );

    if (oWeight.val() == 'NaN')
    {
    oWeight.val() = 0.00;
    }   else    {
            return oWeight.val();

        }

        if ( !helper.isNumber( oWeight.val() ) || parseFloat( oWeight.val() ) < 0 || parseFloat( oWeight.val() ) > 1 )
        {
            $j( this ).css( { 'background-color': '#FFD1D1' } );
            blIsError = true;
        }

        decWeight = decWeight + parseFloat( oWeight.val() );
        console.log( 'Self-Defined (' + row_index.toString() + ') ' + decWeight.toString() );
    } );
$j('#'+config.TableGridNames._02_SelfDefined+'t正文tr td:n子项('+config.SelfDefinedGrid.Edit_Weight_Idx+'))。每个(函数()
{
var row_index=$j(this).closest('tr').index();
/*跳过标题行;return相当于continue*/
如果(row_index==0){return;}
$j(this.css({'background color':'white'});
var oWeight=$j(this.find('input');
if(oWeight.val()=='NaN')
{
oWeight.val()=0.00;
}否则{
返回oWeight.val();
}
如果(!helper.isNumber(oWeight.val())| | parseFloat(oWeight.val())<0 | | parseFloat(oWeight.val())>1)
{
$j(this.css({'background color':'#FFD1D1'});
blIsError=true;
}
decWeight=decWeight+parseFloat(oWeight.val());
log('Self-Defined('+row_index.toString()+')'+decWeight.toString());
} );

如果您试图将0.0分配给函数调用,则会导致问题


if (oWeight.val() == 'NaN') {
    oWeight.val() = 0.0;
} else {
    return oWeight.val();
}

下面是上述代码的修复程序


let val = 0.0;
if (oWeight.val() == 'NaN') {
    val = 0.0;
} else {
    val = oWeight.val();
}

更紧凑的解决方案


let val = oWeight.val() == 'NaN' ? 0.0 : oWeight.val();


然后,在这两个示例中,您都可以使用
val
代替
oWeight.val()

此外,如果您能够重写oWeight.val()函数本身,您可以添加一些检查以确保它始终返回一个数字。不过,我需要查看更多关于该解决方案的代码