点击后每次Javascript事件都会增加

时间:2016-03-23 09:01:07

标签: javascript jquery ractivejs

我在菜单中有一些锚标记作为列表项,在每个项目上我添加了一个名为patient-on-change的类

<a class="patient-on-change" href="/${controllerName}/{{type}}/{{originId}}">

在准备好文档时,我添加了一些代码如下:

$('a.patient-on-change').on('click', function(e){
            if (window.localStorage.getItem('isStopWatchReset') == 'false' || window.localStorage.getItem('isStopWatchReset') == false) {
                var confirmStr = "You are about to change the patient. If change, the data will be deleted. Do you want to Continue ?";
                if (!confirm(confirmStr)) {
                    e.preventDefault();
                } else {
                    var stopWatch = new StopWatch();
                    stopWatch.stopCountingOnDisplay();
                    stopWatch.resetDisplays();
                    window.localStorage.setItem('isStoppedStopWatch', false);
                    window.localStorage.setItem('isStopWatchReset', true);
                }
            }
        });

我的链接在这里生成&gt;&gt;&gt;

    {{#each nodeList}}
    {{#detail}}
    <li>
    {{#if mayBeLeaf}}
        <a href="/${controllerName}/{{type}}/{{originId}}" class="hasGlyphicon{{#if noLink}} menuNoLink{{/if}}">
            {{nodeNm}}
            <span class="glyphicon glyphicon-expand pull-right" title="Click icon to expand menu"
                    data-action="expand"
                    data-ctrl="${controllerName}"
                    data-node-id="{{nodeId}}"<%-- node-id translates to nodeId in the JavaScript data property --%>
                    data-origin-id="{{originId}}"<%-- origin-id translates to originId in the JavaScript data property --%>
                    data-type="{{type}}"
                    data-level="{{level}}"
                    data-row="{{row}}"></span>
        </a>
    {{else}}
        <a class="patient-on-change" href="/${controllerName}/{{type}}/{{originId}}">
        {{#if detail.flagDt}}<span title="Care Notes Complete" class="glyphicon glyphicon-ok-sign"></span>{{/if}} {{nodeNm}}</a>
    {{/if}}
    </li>
    {{/detail}}
{{/each}}

现在的问题是,在第一次单击某个项目时,它没有触发该操作。但是从第二次触发动作,但每次我点击一个项目时alert每次都会增加1。我不能在这里使用e.preventDefault(),因为我需要运行一些代码。有人可以帮我这个吗?

1 个答案:

答案 0 :(得分:-1)

您的代码获得了不必要的}。它应该是这样的

$('a.patient-on-change').on('click', function(e){
    alert('hello');
});