javascript - jquery-edit-in-place动态传递参数
问题描述
我使用 jquery 插件:https ://code.google.com/archive/p/jquery-in-place-editor/ 。在下一节中,我使用“JEIP”而不是全名。
我尝试通过 css 类将 JEIP 不绑定到 ID 而是绑定到许多对象。
<div class="jeip" id='key1' data-type='elephant'>Text 1</div>
<div class="jeip" id='key2' data-type='mouse'>Text 2</div>
现在我也想动态传递数据类型元素。我想出了“参数”选项来传递额外的数据。但它似乎不是动态的。
要初始化 JEIP,我使用:
$(".editInPlace").editInPlace({
url: "http://submitUrl/",
params: "datatype="+$(this).data('type'),
callback: function(){
console.log( $(this).data('type') );
},
}
但是参数是错误的。我只在接收提交操作的服务器脚本中得到 undifiend。当我使用回调函数时,我可以获得带有正确数据的控制台输出。如何将数据元素传递给服务器?
感谢帮助。
解决方案
假设所有元素在脚本执行时都已就位(即它们不会在以后动态添加),您可以简单地循环元素并使用适当的值调用 editInPlace 插件函数:
$('.editInPlace').each(function(i, el){
var $el = $(el); // cache the jQuery object corresponding to the current element
var dataType = $el.data('type');
// call the editInPlace plugin directly on the element
$el.editInPlace({
url : 'http://submitUrl/',
params : 'datatype=' + dataType,
callback : function(){
console.log(dataType);
}
});
});
推荐阅读
- node.js - 哪个是好习惯?在数据库中公开主键或使用其他唯一键查找用户?
- r - 如何在r中搜索特定数量的大于0的连续值?
- json - 如何修复:“SyntaxError:JSON.parse:JSON 数据的第 1 行第 1 列的数据意外结束”
- arrays - Flutter 如何在共享首选项中存储具有相同变量的值列表,然后从那里获取特定值
- python - 截至今天,我的不和谐机器人无法找到我的成员
- windows - 为什么在我执行 pm2 进行服务器端渲染后删除了 .nuxt 中的 dist 文件夹?
- android - 使用 ionic 在 esc/pos 移动打印机中连接和打印
- python - 从 python 调用时如何捕获 git bash 的命令行打印输出?
- objective-c - iOS:块捕获一个值
- reactjs - 以外部 api 为前缀的任何地方的 CORS 都需要 400 标头