未捕获的ReferenceError:赋值中的左侧无效

时间:2012-08-06 09:11:38

标签: javascript

我从JavaScript代码收到此错误:

Uncaught ReferenceError: Invalid left-hand side in assignment 

这是我的代码:

<script type="text/javascript">
function repartition()
{
    var principal_p             = <?php echo $data23['principal_p'] ;?>; 
    var principal_c             = <?php echo $data23['principal_c'] ;?>; 
    var interets_p              = <?php echo $data23['interets_p'] ;?>; 
    var interets_c              = <?php echo $data23['interets_c'] ;?>; 
    var clause_penale_p         = <?php echo $data23['clause_penale_p'] ;?>; 
    var clause_penale_c         = <?php echo $data23['clause_penale_c'] ;?>;
    var domages_interets_p      = <?php echo $data23['domages_interets_p'] ;?>;
    var domages_interets_c      = <?php echo $data23['domages_interets_c'] ;?>;
    var art700_cpc_p            = <?php echo $data23['art700_cpc_p'] ;?>;
    var art700_cpc_c            = <?php echo $data23['art700_cpc_c'] ;?>;
    var art475_1_cpp_p          = <?php echo $data23['art475_1_cpp_p'] ;?>;
    var art475_1_cpp_c          = <?php echo $data23['art475_1_cpp_c'] ;?>;
    var art_441_6_cc_p          = <?php echo $data23['art_441_6_cc_p'] ;?>;
    var art_441_6_cc_c          = <?php echo $data23['art_441_6_cc_c'] ;?>;
    var frais_ar_p              = <?php echo $data23['frais_ar_p'] ;?>; 
    var frais_ar_c              = <?php echo $data23['frais_ar_c'] ;?>;
    var agios_p                 = <?php echo $data23['agios_p'] ;?>; 
    var agios_c                 = <?php echo $data23['agios_c'] ;?>;
    var depens_p                = <?php echo $data23['depens_p'] ;?>; 
    var depens_c                = <?php echo $data23['depens_c'] ;?>;   
    var frais_execution_p       = <?php echo $data23['frais_execution_p'] ;?>; 
    var frais_execution_c       = <?php echo $data23['frais_execution_c'] ;?>;  
    var contrib_aid_juridiq_p   = <?php echo $data23['contrib_aid_juridiq_p'] ;?>;
    var contrib_aid_juridiq_c   = <?php echo $data23['contrib_aid_juridiq_c'] ;?>;  
    var frais_greffe_p          = <?php echo $data23['frais_greffe_p'] ;?>;
    var frais_greffe_c          = <?php echo $data23['frais_greffe_c'] ;?>;
    principal = document.getElementsByName("principal_s");
    document.getElementsById(nprincipal)= (principal*principal_p)/100;
}
</script>

导致错误的行是其中包含document.getElementsById的行。为什么会导致错误?

5 个答案:

答案 0 :(得分:4)

您正在尝试分配函数调用的结果,该函数调用从不是可以分配给的参考,而是分配值,因此您无法分配它。这就是你得到那个错误的原因。


一般来说,修复代码,这不在范围之内。我可以帮助你做一些猜测:

您可以将元素的.innerHTML设置为如下值:

   document.getElementById("nprincipal").innerHTML = (principal[0].value*principal_p)/100;

我也修了:

  • getElementsById应为getElementById
  • principal是我认为是单个输入元素的实时节点列表,因此我按.value
  • 获取[0].value
  • getElementById中使用字符串,因为nprincipal未在您的代码中定义

答案 1 :(得分:1)

您在代码中尝试做什么?目前,您要将计算结果分配给DOM元素,如果要在页面上显示结果,那么您可能希望这样做:

document.getElementById(nprincipal).innerHTML = (principal*principal_p)/100;

答案 2 :(得分:0)

document.getElementsById(nprincipal)= (principal*principal_p)/100;

是错误......如果它是某个表单元素,它应该是

document.getElementById(nprincipal).value= (principal*principal_p)/100;

修改

另外

principal = document.getElementByName("principal_s");

应该是

principal = document.getElementByName("principal_s").value;

答案 3 :(得分:0)

扩展我对操作的评论。

查看json和javascript对象。这将节省您将数据从php解析为javascript的麻烦:

var data23 = <?php echo json_encode($data23) ;?>

这将使对象看起来像这样:

{
    principal_p: <?php echo $data23['principal_p'] ;?>,
    principal_c: <?php echo $data23['principal_c'] ;?>,
    interets_p: <?php echo $data23['interets_p'] ;?>, 
    interets_c: <?php echo $data23['interets_c'] ;?>,

    ....
}

您现在可以引用data23.principal_p来获取从<?php echo $data23['principal_p'] ;?>解析的数据。

这不是答案,因为当您尝试将计算分配到DOM节点时,我可以看到您遗漏了某些内容。

PHP.net json_encode

答案 4 :(得分:0)

使用此:

document.getElementsById("nprincipal").value = (principal*principal_p)/100;

document.getElementsById("nprincipal").innerHTML = (principal*principal_p)/100;