试图获取jQuery中每一行的总成本

Trying to get total costs in each row jQuery

本文关键字:一行 总成本 获取 jQuery      更新时间:2023-09-26

嗨,我正试图从每一行中获得总成本。下面是一张截图:

链接:https://i.stack.imgur.com/4ZgLt.png

正如你所看到的,一行已经通过乘积5*2计算出了它的总数。但这只发生在第一行,但我很乐意在每一行中进行计算。我认为这是因为每行中的名称变量相同,这就是为什么javascript无法计算每行的总数。我想我需要一个for循环来解决这个问题。但我真的不知道怎么做。这是我目前拥有的代码:

@foreach ($project->projecttask as $pt)
              <tr>
                <td>
                    {{ $pt->task['task_name'] }}    
                </td>
                <td>
                    <input type="hidden" name="projectId" value="{{$project->id}}"/>
                    {{Form::hidden('hour', $project->hour)}}
                </td>
                <td>
                    {{ Form::text('hour', $pt->hour, array('class' => 'form-control', 'id'=>'hour')) }}
                </td>
                <td>
                    {{ Form::text('hour_salary', $pt->hour_salary, array('class' => 'form-control', 'id'=>'hour_salary')) }}
                </td>
                <td>
                {{ Form::text('total_salary', $pt->total_salary, array('class' => 'form-control', 'id'=>'total_salary', 'disabled')) }}
                {{--    {{ Form::text('total_salary', $project->total_salary, array('class' => 'form-control', 'id'=>'total_salary', 'disabled')) }}--}}
                </td>
                <td>
                    <div class="dropdown">
                        <button type="button" class="btn btn-danger dropdown-toggle selDelete" data-toggle="dropdown">
                            <input id="check1" name="checkbox[]" type="checkbox" class="check" >
                            <span class="caret-hover caret"></span>
                        </button>
                        <ul class="dropdown-menu" aria-labelledby="selDelete" role="menu">
                            <li><a href= "{{ route('user.projecttasks.destroy',array( $pt->id )) }}" data-method="delete" >Delete</a></li>                
                        </ul>   
                     </div>
                </td>
              </tr>
@endforeach
<script>
$(function(){
    updateTotal();
});
    var updateTotal = function(){
        var hour_salary = parseFloat($("#hour_salary").val()); // get number as float
        // alternately parseInt(string, 10), in case you work with integers
        var hour = parseFloat($("#hour").val());
        if (!isNaN(hour_salary)) { // the input is a number
            $("#total_salary").val(hour_salary * hour); // update second field
        } else { // the input wasn't a number
            $("#total_salary").val("not a number?"); // show an error mesage
        }
    };
        // we used jQuery 'keyup' to trigger the computation as the user type
    $("#hour_salary").keyup(function() { // when key is released in "#inputfield1"
    // "change()" is also possible instead of "keyup()", slightly different behavior
        updateTotal();
    });
        // we used jQuery 'keyup' to trigger the computation as the user type
    $("#hour").keyup(function() { // when key is released in "#inputfield1"
    // "change()" is also possible instead of "keyup()", slightly different behavior
        var hour_salary = parseFloat($("#hour_salary").val()); // get number as float
        // alternately parseInt(string, 10), in case you work with integers
        var hour = parseFloat($("#hour").val());
        if (!isNaN(hour_salary)) { // the input is a number
            $("#total_salary").val(hour_salary * hour); // update second field
        } else { // the input wasn't a number
            $("#total_salary").val("not a number?"); // show an error mesage
        }
    });
  </script>

有人能帮我吗?

是的,首先你应该在这里使用类而不是ID。在同一页面上只能有唯一的ID。

其次,您可能希望通过获取事件的当前行并查找其中要更改的元素,将计算限制在同一行。

第三,你不需要分离你的keyup函数,这只会让事情变得更容易。事实上,由于第二个keyup函数是updateTotal的副本,因此它完全没有必要:

// Added a variable that this function accepts, "row", so it knows its location in the document
var updateTotal = function(row){
    // Define all elements as descendants of the "row" passed in
    var salary = row.find(".hour_salary"),
        hour = row.find(".hour"),
        total = row.find(".total_salary");
    var salaryVal = parseFloat(salary.val());
    var hourVal = parseFloat(hour.val());
    // Added another check for hourVal being a number as well
    if (!isNaN(salaryVal) || !isNaN(hourVal)) {
        total.val(salaryVal * hourVal);
    } else {
        total.val("not a number?");
    }
};
//Run same keyup function for both text boxes
$(".hour_salary, .hour").keyup(function() {
    // Find the row
    var row = $(this).closest('tr');
    // Pass in the row
    updateTotal(row);
});

注意:我已经将所有的ID选择器都更改为类,所以在HTML中反映出来之前,这将不起作用。

希望这能有所帮助。

有点晚了,但我已经用以下代码修复了这个问题(对于感兴趣的人):

@foreach ($project->projecttask as $pt)
              <tr>
                    <td>
                        {{ Form::select('id_task', array('0' => 'no task selected', 'tasks'=>Task::lists('task_name','id') ), null, array('class' => 'form-control')) }}
                    </td>
                    <td>
                        {{$pt->hour}}
                        <input type="hidden" name="projectId" value="{{$project->id}}"/>
                    </td>
                    <td>
                        {{ Form::text('hour', $pt['hour'], array('class' => 'form-control hour', 'data-row'=>$pt->id , 'id'=>'hour' . $pt->id )) }}
                    </td>
                    <td>
                        {{ Form::text('hour_salary', $pt->hour_salary, array('class' => 'form-control hour_salary', 'data-row'=>$pt->id , 'id'=>'hour_salary' . $pt->id )) }}
                    </td>
                    <td>
                    {{ Form::text('total_salary', $pt->total_salary, array('class' => 'form-control', 'id'=>'total_salary' . $pt->id, 'disabled')) }}
                    </td>
                    <td>
                        <div class="dropdown">
                            <button type="button" class="btn btn-danger dropdown-toggle selDelete" data-toggle="dropdown">
                                <input id="check1" name="checkbox[]" type="checkbox" class="check" >
                                <span class="caret-hover caret"></span>
                            </button>
                          {{  $pt->id }}
                            <ul class="dropdown-menu" aria-labelledby="selDelete" role="menu">
                                <li><a href= "{{ route('user.projecttasks.destroy',array( 'id' => $pt->id, 'project' => $project->id )) }}" data-method="delete" >Delete</a></li>
                            </ul>   
                         </div>
                    </td>
              </tr>
            @endforeach
<script>
$(function(){
    //$('#test').css('color','red');
    updateTotal();
});
    var updateTotal = function(rowId){
        var rowId = rowId,
            $hour_salary =  $("#hour_salary" + rowId),
            $total_salary =  $("#total_salary" + rowId);
        var hour = parseFloat($("#hour" + rowId).val()),
            hour_salary = parseFloat($hour_salary.val()); 
        if ( hour_salary.length > 0 ) 
            $total_salary.val("0"); 
        else
            $total_salary.val( hour_salary * hour );
    };
    $(".hour_salary").keyup(function() { 
        var $this = $(this);
        //wrm row ipv data-row?
        var rowId = $this.data('row');
        $this.val( $this.val().replace(/'D/g,'') );
        updateTotal( rowId );
    });
    $(".hour").keyup(function() { 
        var $this = $(this);
        var rowId = $this.data('row');
        $this.val( $this.val().replace(/'D/g,'') );
        updateTotal( rowId );
    });
  </script>

老实说,我得到了实习主管的帮助,制定了这个代码。