[英]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.