This article mainly introduces the relevant information summarized by several methods of transferring and obtaining values in WeChat mini programs. Here are detailed explanations of these methods, and sample codes are attached. Friends in need can refer to it
WeChat applet passing value
The common values in the applet are as follows. If you write down a complete project, the probability of using it is almost 100%.
List index subscript value
Page value passing
form form value
1. List index subscript value
The implementation method is: data-index="{{index}}" digging and e .currentTarget.dataset.index to fill in the pit
1.1 Generate value
<image src="../../../images/icon_delete.png" /><text>删除</text>
Add data-index="{{index to the delete icon and text }}"Custom attributes and binding click events bindtap="delete"
<image src="../../../images/icon_delete.png" /><text>删除</text>
Implement the delete method and get the index subscript value.
delete: function (e) { var index = parseInt(e.currentTarget.dataset.index); console.log("index" + index); }
What happens if e.target is used instead of e.currentTarget?
will cause the index value to be output only if the
Then what is the use of target? It is used to distinguish sub-elements from external elements when they need to be processed separately, such as when changing the user's avatar, click on the avatar itself to preview the large image, and Click the entire line where the avatar is to switch the avatar.
For detailed explanation of the difference between the two, please see the document: https://mp.weixin.qq.com/debug/wxadoc/dev/framework/view/wxml /event.html
1.2 Get the value
Try to find the corresponding element deletion address from the index data
// 找到当前地址AVObject对象 var address = that.data.addressObjects[index]; // 给出确认提示框 wx.showModal({ title: '确认', content: '要删除这个地址吗?', success: function(res) { if (res.confirm) { // 真正删除对象 address.destroy().then(function (success) { // 删除成功提示 wx.showToast({ title: '删除成功', icon: 'success', duration: 2000 }); // 重新加载数据 that.loadData(); }, function (error) { }); } } })
2 .Page value
Pass the address id from the shipping address list page to the edit page to read the original address for modification.
The address/list page implements the following code
<view class="container" data-index="{{index}}" bindtap="edit"><image src="../../../images/icon_edit.png" /><text>编辑</text></view> edit: function (e) { var that = this; // 取得下标 var index = parseInt(e.currentTarget.dataset.index); // 取出id值 var objectId = this.data.addressObjects[index].get('objectId'); wx.navigateTo({ url: '../add/add?objectId='+objectId }); },
The address/add page implements the onLoad(options) method to obtain the objectId from the url path
onLoad: function (options) { var objectId = options.objectId }
Then it’s time to access the network and render the page.
3. Form form value
3.1 Method 1, through