如何通过单击按钮调用JavaScript函数并在循环文本框中执行操作?

如何通过单击按钮调用JavaScript函数并在循环文本框中执行操作?,javascript,php,mysql,sum,Javascript,Php,Mysql,Sum,我是javascript新手,我想知道如何通过单击按钮对表中的每行执行一些数学操作 列“Variable”中的文本框是由while循环创建的,我想对两个值进行迭代操作,一个值来自数组中的第一个文本框(变量1),另一个值来自第二个文本框(变量2),因此我需要根据按钮的不同对这两个数字进行求和或减法 如何通过单击按钮使javascript函数调用它 在我的示例中,我有一个“Case语句”从表中获取id “操作”,其中,如果id为1,则必须求和;如果id为2,则必须求和 减法,3必须进行乘法运算 例如

我是javascript新手,我想知道如何通过单击按钮对表中的每行执行一些数学操作

列“Variable”中的文本框是由while循环创建的,我想对两个值进行迭代操作,一个值来自数组中的第一个文本框(变量1),另一个值来自第二个文本框(变量2),因此我需要根据按钮的不同对这两个数字进行求和或减法


如何通过单击按钮使javascript函数调用它

在我的示例中,我有一个“Case语句”从表中获取id “操作”,其中,如果id为1,则必须求和;如果id为2,则必须求和 减法,3必须进行乘法运算

例如,我有三行:


  • 指示符1=变量1+变量2-->单击按钮---->显示求和结果
  • Indicator 2=变量3-变量4-->单击按钮---->显示减法的结果
  • Indicator 3=变量5*变量6-->单击按钮---->显示乘法结果
  • 等等

    你可以看到我的桌子。

            <form action="ingreso-datos-exe.php" method="post"> 
                  <input type="hidden" value="<?php echo $iduser;?>"  name="iduser"/>
             <table id="tabla-registros" class="table table-striped table-bordered responsive">
       <thead>
            <tr>
             <th>Indicador</th>
              <th>Variables</th>
               <th>Result</th>
              <th>Compute Operation</th>
              </tr>
              </thead>
              <tbody>
    
    
    
            <?php
    
            //to get the indicator's name in the first column of the table
    
            while($r = mysql_fetch_assoc($checkSQL)) {
                    $id_indicador= $r['id_indicador'];
                    $nombre= $r['nombre_indicador'];
                    $id_calculo= $r['id_calculo'];
                    echo $id_indicador.$nombre.'</br>'; ?>
    
            <td><?php echo $id_indicador.$nombre;?></td><td>
              <table>
             <tr>
    
            <?php   
    
                // To get the variable's name in the second column of the table     
                while($r2 = mysql_fetch_assoc($checkSQL2)) {
                    $nombre_variable= $r2['nombre_variable_medicion'];
                    $idvariable= $r2['id_variablemedicion'];
                    //  var_dump($r2);
                                                 ?>
    
        <th><?php echo $nombre_variable;?></th>
        <td width=60px><input type="text" value="" class="price" name="valor[<?php echo $idvariable; ?>]"/></td> 
    
        <input type="hidden" value="<?php echo $id_indicador;?>"  name="id_indicador[<?php echo $idvariable; ?>]"/>
       <input type="hidden" value="<?php echo $idvariable;?>"  name="id_variable[<?php echo $idvariable; ?>]"/>
    
          <?php     } ?>
    
    
                            </tr>
                            <tr>
    
               </tr> 
    
              </table>
    
      <?php switch($id_calculo){ 
    
                                case '1':?>
                                    <td><input class="btn btn-success" type="button" name="btnSum" value="Sum"    OnClick="fncSum();"></td>
    
    
                               <?php  break; ?>
    
    
                              <?php case '2':?>
                              <td><input class="btn btn-success" type="button" name="btnSum" value="substract" OnClick="fncSum();"></td>
    
    
                               <?php  break; ?>
    
                              <?php case '3':?>
                             <td><input class="btn btn-success" type="button" name="btnSum" value="multiplication" OnClick="fncSum();"></td>
    
    
                              <?php  break;
                               } ?>
                               <?php    echo '</td>'.'</tr>';
                              }
                                    ?>                  
    
    
    
    </tbody>
    
    </table>
    
    
            </form> 
    

    如果我错过了什么,请告诉我

    此代码显示上表。

            <form action="ingreso-datos-exe.php" method="post"> 
                  <input type="hidden" value="<?php echo $iduser;?>"  name="iduser"/>
             <table id="tabla-registros" class="table table-striped table-bordered responsive">
       <thead>
            <tr>
             <th>Indicador</th>
              <th>Variables</th>
               <th>Result</th>
              <th>Compute Operation</th>
              </tr>
              </thead>
              <tbody>
    
    
    
            <?php
    
            //to get the indicator's name in the first column of the table
    
            while($r = mysql_fetch_assoc($checkSQL)) {
                    $id_indicador= $r['id_indicador'];
                    $nombre= $r['nombre_indicador'];
                    $id_calculo= $r['id_calculo'];
                    echo $id_indicador.$nombre.'</br>'; ?>
    
            <td><?php echo $id_indicador.$nombre;?></td><td>
              <table>
             <tr>
    
            <?php   
    
                // To get the variable's name in the second column of the table     
                while($r2 = mysql_fetch_assoc($checkSQL2)) {
                    $nombre_variable= $r2['nombre_variable_medicion'];
                    $idvariable= $r2['id_variablemedicion'];
                    //  var_dump($r2);
                                                 ?>
    
        <th><?php echo $nombre_variable;?></th>
        <td width=60px><input type="text" value="" class="price" name="valor[<?php echo $idvariable; ?>]"/></td> 
    
        <input type="hidden" value="<?php echo $id_indicador;?>"  name="id_indicador[<?php echo $idvariable; ?>]"/>
       <input type="hidden" value="<?php echo $idvariable;?>"  name="id_variable[<?php echo $idvariable; ?>]"/>
    
          <?php     } ?>
    
    
                            </tr>
                            <tr>
    
               </tr> 
    
              </table>
    
      <?php switch($id_calculo){ 
    
                                case '1':?>
                                    <td><input class="btn btn-success" type="button" name="btnSum" value="Sum"    OnClick="fncSum();"></td>
    
    
                               <?php  break; ?>
    
    
                              <?php case '2':?>
                              <td><input class="btn btn-success" type="button" name="btnSum" value="substract" OnClick="fncSum();"></td>
    
    
                               <?php  break; ?>
    
                              <?php case '3':?>
                             <td><input class="btn btn-success" type="button" name="btnSum" value="multiplication" OnClick="fncSum();"></td>
    
    
                              <?php  break;
                               } ?>
                               <?php    echo '</td>'.'</tr>';
                              }
                                    ?>                  
    
    
    
    </tbody>
    
    </table>
    
    
            </form> 
    
    
    
    //返回按钮的父行
    函数getRow(el){
    var row=el.parentNode.parentNode;
    返回行;
    }
    //从val1返回数字
    函数getVal1(行){
    return+row.querySelector(“.val1”).value | | 0
    }
    //从val2返回数字
    函数getVal2(行){
    return+row.querySelector(“.val2”).value | | 0
    }
    //设定结果
    函数setResult(行,值){
    row.querySelector(“.result”).value=val
    }
    //行动
    //从数据操作属性读取操作
    函数动作(){
    var a=此.getAttribute(“数据操作”);
    var row=getRow(此);
    var val1=getVal1(行);
    var val2=getVal2(行);
    var-res=0;
    开关(a){
    案例“金额”:
    res=val1+val2;
    打破
    “减项”一案:
    res=val1-val2;
    打破
    案例“乘法”:
    res=val1*val2;
    打破
    违约:
    抛出新错误(“未知操作“+a+””);
    }
    setResult(行,res);
    }
    //加载文档后,为每个btn操作元素添加单击事件
    addEventListener(“加载”,函数(){
    var btns=document.queryselectoral(“.btn action”);
    对于(var i=0;i
    
    //返回按钮的父行
    函数getRow(el){
    var row=el.parentNode.parentNode;
    返回行;
    }
    //从val1返回数字
    函数getVal1(行){
    return+row.querySelector(“.val1”).value | | 0
    }
    //从val2返回数字
    函数getVal2(行){
    return+row.querySelector(“.val2”).value | | 0
    }
    //设定结果
    函数setResult(行,值){
    row.querySelector(“.result”).value=val
    }
    //行动
    //从数据操作属性读取操作
    函数动作(){
    var a=此.getAttribute(“数据操作”);
    var row=getRow(此);
    var val1=getVal1(行);
    var val2=getVal2(行);
    var-res=0;
    开关(a){
    案例“金额”:
    res=val1+val2;
    打破
    “减项”一案:
    res=val1-val2;
    打破
    案例“乘法”:
    res=val1*val2;
    打破
    违约:
    抛出新错误(“未知操作“+a+””);
    }
    setResult(行,res);
    }
    //加载文档后,为每个btn操作元素添加单击事件
    addEventListener(“加载”,函数(){
    var btns=document.queryselectoral(“.btn action”);
    
    对于(var i=0;i
    ,基本上…“我如何通过单击按钮使javascript函数调用它?”什么是“它”?请澄清。此外,我建议您开始正确缩进和格式化代码。分析起来非常混乱。@Alexandre好的,我现在就做,“它”=是javascript函数,因为我不知道如何对循环文本框求和,每个“indicator”都有自己的数学运算,你能帮我吗?@MarcB你知道如何使函数对循环文本框求和吗?
    ,基本上……“我如何让javascript函数通过单击按钮来调用它?”确切的“it”是什么?请澄清。另外,我建议您开始正确地缩进和格式化代码。分析起来非常混乱。@Alexandre好的,我现在就做,“它”=是javascript函数,因为我不知道如何对循环文本框和每个“指示符”求和它有自己的数学运算,你能帮我吗?@MarcB你知道如何使函数对循环文本框求和吗?
    ,基本上…“我如何使javascript函数通过单击按钮来调用它?”确切的“它”是什么?请澄清。另外,我建议您开始正确地缩进和格式化代码。分析起来非常混乱。@Alexandre好的,我现在就做,“它”=是javascript函数,因为我不知道如何对循环文本框和每个“指示符”求和有自己的数学运算,你能帮我吗?@MarcB你知道如何使函数对循环文本框求和吗?greaaaa不实现输入文本的名称就可以执行这些运算,因为在我的情况下,名称是相同的。我不能这样做res=val1+val2;因为我只有一个输入文本,但当我使用当循环显示TwoGreaaat时,可以在不实现输入文本名称的情况下执行操作,因为在我的例子中,输入文本的名称