Onload函数不断添加到数组而不清除它

时间:2019-05-18 17:41:40

标签: javascript jquery

可能是一个愚蠢的问题。我的jsp中有以下js代码。问题是,尽管我试图在每次加载时清除数组 markerLong,markerLat ,但并没有清除它们,而是在刷新页面时保持附加元素。

<script>


// Gmaps Maps
// ------------------------------

$(window).on("load", function(){
// Map Markers
    // ------------------------------

    map = new GMaps({
        div: '#markers',
        lat: 18.520430,
        lng: 73.856743,
        styles: [{"featureType":"administrative.locality","elementType":"all","stylers":[{"hue":"#2c2e33"},{"saturation":7},{"lightness":19},{"visibility":"on"}]},{"featureType":"landscape","elementType":"all","stylers":[{"hue":"#ffffff"},{"saturation":-100},{"lightness":100},{"visibility":"simplified"}]},{"featureType":"poi","elementType":"all","stylers":[{"hue":"#ffffff"},{"saturation":-100},{"lightness":100},{"visibility":"off"}]},{"featureType":"road","elementType":"geometry","stylers":[{"hue":"#bbc0c4"},{"saturation":-93},{"lightness":31},{"visibility":"simplified"}]},{"featureType":"road","elementType":"labels","stylers":[{"hue":"#bbc0c4"},{"saturation":-93},{"lightness":31},{"visibility":"on"}]},{"featureType":"road.arterial","elementType":"labels","stylers":[{"hue":"#bbc0c4"},{"saturation":-93},{"lightness":-2},{"visibility":"simplified"}]},{"featureType":"road.local","elementType":"geometry","stylers":[{"hue":"#e9ebed"},{"saturation":-90},{"lightness":-8},{"visibility":"simplified"}]},{"featureType":"transit","elementType":"all","stylers":[{"hue":"#e9ebed"},{"saturation":10},{"lightness":69},{"visibility":"on"}]},{"featureType":"water","elementType":"all","stylers":[{"hue":"#e9ebed"},{"saturation":-78},{"lightness":67},{"visibility":"simplified"}]}]
    });
    let markerLat,markerLong; 
    markerLat = [
        <c:forEach var="s" items="${list}">
            <c:out value="${s.currentLat}"/>,
        </c:forEach>
    ];
    markerLong = [
      <c:forEach var="s" items="${list}">
          <c:out value="${s.currentLong}"/>,
      </c:forEach>
    ];
      console.log(markerLong.length);
      console.log(markerLat.length);


markerLong=[];
markerLat=[];
});
</script>

如何确保每次刷新页面时都将旧条目从数组中删除。

2 个答案:

答案 0 :(得分:1)

页面加载后,您可以尝试调用下面的函数。

<form action="/store-absence" method="POST">
    <table>
        {{ csrf_field() }}
        <thead>
        <tr role="row">
            <th>Name</th>
            <th>Date</th>
            <th>Attended</th>
        </thead>
        <tbody>

        <?php date_default_timezone_set('Asia/Baghdad') ?>
        @foreach ($students as $student)
        <tr>
            <input type="hidden" name="student_id[]" id="" value="{{$student->id}}">
            <input type="hidden" name="date" value="{{date('Y-m-d')}}">
            <td colspan=""> {{$student->name}} </td>
            <td colspan=""> {{date('Y-m-d')}} </td>
            <td colspan="1">
                <input style="border:none" type="checkbox" name="attended[]" id="">
            </td>
        </tr>
        @endforeach

        </tbody>

    </table>

    <button type="submit" class="btn btn-block btn-primary btn-lg">
        Insert
    </button>
</form>

答案 1 :(得分:0)

正如我所说,我犯了一个非常愚蠢的错误。 Js一切正常。它是Java控制器,可在此处继续向列表中添加元素。