简体   繁体   English

如何使用angularjs或jquery将每个单击的按钮动作保存在json文件/对象中?

[英]How to save each and every clicked button action in json file/object using angularjs or jquery?

I wanted to save each and every button action(for ng-clicked and non-ng-clicked including file uploads/urls ie whatever I click, that button should be stored in json file/object ie for all <input type="button"> in my case.) in my json file/object using either angularjs or jquery ? 我想保存每个按钮动作(对于ng单击的和非ng单击的,包括文件上传/ URL,即无论我单击什么,该按钮应存储在json文件/对象中,即用于所有<input type="button">以我为例。)使用angularjs或jquery在我的json文件/对象中? How can I do it ? 我该怎么做 ? Created Fiddle ,Please help me. 创建了Fiddle ,请帮助我。 Thanks in advance. 提前致谢。

html:

You can add global beforeSend handler using: 您可以使用以下命令添加全局的beforeSend处理程序:

$.ajaxSetup({
  beforeSend: function(jqXHR, settings) {
    var data = {
       url: settings.url,
       data: settings.data
    };
    $.ajax({url: 'save_json.php', data: data, beforeSend: $.noop);
  }
});

This will work only for ajax requests, if you want to log form submit you will need to add global submit handler: 这仅适用于ajax请求,如果要记录表单提交,则需要添加全局提交处理程序:

$('body').on('submit', 'form', function(e) {
  var $target = $(e.target);
  $.ajax({
    url: 'save_json.php',
    data: {url: $target.attr('action')},
    beforeSend: $.noop, 
    sucess: function() {
      $target.submit(function(e) {
        // prevent global handler
        e.stopPropagation();
      }).submit();
    }
  });
  return false;
});

Set click listener on the form element. 在表单元素上设置点击监听器。

$('#formid').click(function (e) {
    //e.target will give the element clicked inside the form
});

Check if the e.target is a button element and save the button in your json file/object. 检查e.target是否为按钮元素,并将按钮保存在json文件/对象中。

I think the most angular-way of achieving this is to create a directive and use this directive instead of <input type="button"> I created a sample; 我认为实现此目标的最有角度的方法是创建一个指令,并使用该指令代替我创建的示例<input type="button">

var actionLogs = [];

yourModule.directive("actionButton", [function () {
return {
    scope: {
        action: '&',
        type: '@'
    },
    restrict: 'E',
    replace: true,
    transclude: true,
    controller: function ($scope, $element) {

        var logClickEvent = function ($event) {
            var log = {element: $element, action: $scope.action, clickTime: new Date()};
            actionLogs.push(log);
        };

        $scope.doAction = function ($event) {

            //** Do whatever you want here before every action
            logClickEvent($event);

            $scope.action.apply(null, []);
        };
    },
    link: function (scope, element, attrs) {
        if (typeof attrs.type === "undefined") {
            scope.type = "button";
        }
    },
    template: '<input type="{{type}}" ng-click="doAction($event)"><span ng-transclude></span></input>'
};
}]);

You can use <action-button action="yourNgClickFunc()"> instead of <input type="button" ng-click="yourNgClickFunc()"> and your clicks will be logged in the array actionLogs . 您可以使用<action-button action="yourNgClickFunc()">代替<input type="button" ng-click="yourNgClickFunc()">并且您的点击将记录在actionLogs数组中。

In some cases you may want to log click events other then <input type="button"> s. 在某些情况下,您可能希望记录除<input type="button">的点击事件。 Then I prefer to use an attribute directive instead of element directive, for example; 然后,例如,我更喜欢使用属性指令而不是元素指令。

var actionLogs  = [];

yourModule.directive("loggable", [function () {
return {
    restrict: 'A',
    link: function (scope, element, attrs) {

        element.click(function (e) {
            var action = scope[attrs.ngClick.split("(")[0]];
            var log = {element: element, action: action, clickTime: new Date()};
            actionLogs.push(log);
        });

    }
};
}]);

You can use <input type="button" ng-click="yourNgClickFunc()" loggable> instead of <action-button> and your clicks still will be logged in the array actionLogs 您可以使用<input type="button" ng-click="yourNgClickFunc()" loggable>代替<action-button>并且您的点击仍将记录在actionLogs数组中

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用eihter javascript或jquery或angularjs将每个表单输入按钮动作保存到json文件中? - How to save each and every form input button actions into json file using eihter javascript or jquery or angularjs? 将对象保存到angularjs中的JSON文件中? - Save object to JSON file in angularjs? 如何通过使用javascript或jquery或angularjs将表单输入按钮保存到json文件中? - How to save form input buttons into json file by using either javascript or jquery or angularjs? 如何在使用 jQuery 单击按钮时下载嵌入 PDF 文件? - How to Download Embed PDF File While Button Clicked Using jQuery? 单击项目中的视图时,在JSON文件中定位子对象-AngularJS - Targeting a child object in JSON file when item is in view is clicked - AngularJS 如何在parse.com + AngularJS中使用for循环将每个对象保存在数组中? - How to save every object inside an array using for loop in parse.com + AngularJS? 如何使用jquery来获取所单击的每个按钮中的数据属性 - How to use jquery to grab data-attribute in every button that was clicked 每次在jquery中单击按钮时,如何使输入添加值? - How To make the input add values every time the button is clicked in jquery? 当按钮被单击但被禁用时如何触发对按钮的操作 - how to trigger an action on a button when it is clicked but is disabled w/ jquery 如何使用本地 json 文件 jquery 或 angularjs 为文本框实现自动完成 - How to Implement autocomplete for a textbox using local json file jquery or angularjs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM