0

我从我的 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。为什么会导致错误?

4

5 回答 5

4

您正在尝试分配函数调用的结果,该结果永远不是可以分配的引用,而是一个值,因此您无法分配给它。这就是您收到该错误的原因。


至于一般修复您的代码,这超出了范围。我可以帮你做一些猜测:

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

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

我还修复了:

  • getElementsById应该getElementById
  • principal是我假设是单个输入元素的实时节点列表,所以我.value通过[0].value
  • 在代码中未定义的getElementByIdas中使用字符串nprincipal
于 2012-08-06T09:13:48.507 回答
1

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

document.getElementById(nprincipal).innerHTML = (principal*principal_p)/100;
于 2012-08-06T09:14:41.573 回答
0

用这个:

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

或者

document.getElementsById("nprincipal").innerHTML = (principal*principal_p)/100;
于 2012-08-06T09:23:55.617 回答
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;
于 2012-08-06T09:15:56.577 回答
0

为了扩展我对 op 的评论。

查看 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

于 2012-08-06T09:21:35.167 回答