Home  >  Article  >  WeChat Applet  >  Summarize several methods of passing and obtaining values ​​in the development of WeChat mini programs

Summarize several methods of passing and obtaining values ​​in the development of WeChat mini programs

巴扎黑
巴扎黑Original
2017-09-12 09:19:041955browse

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 89c662c6f8b87e82add978948dc499d2 is clicked, and the click element dc0870658837139040642baa5555a380 or 28f128881ce1cdc57a572953e91f7d0f will output NaN.

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: &#39;确认&#39;,
  content: &#39;要删除这个地址吗?&#39;,
  success: function(res) {
    if (res.confirm) {
      // 真正删除对象
      address.destroy().then(function (success) {
        // 删除成功提示
        wx.showToast({
          title: &#39;删除成功&#39;,
          icon: &#39;success&#39;,
          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(&#39;objectId&#39;);
  wx.navigateTo({
    url: &#39;../add/add?objectId=&#39;+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 8b0563f2b998c6d0b1307ef69602a791 and 0d5a8d767214bf25558ef7380df9603a Tags are used together with the

layout as follows:


<form bindsubmit="formSubmit">
  <input name="detail" placeholder="详情地址" />
  <input name="realname" placeholder="收件人姓名" />
  <input name="mobile" placeholder="手机号码" type="number"/>
  <button formType="submit" type="primary">Submit</button>
</form>

js value:


formSubmit: function(e) {
  // detail
  var detail = e.detail.value.detail;
  // realname
  var realname = e.detail.value.realname;
  // mobile
  var mobile = e.detail.value.mobile;
}

Document source: https://mp.weixin.qq.com/debug/wxadoc/dev/component/form.html

3.2 Method 2,

via943f6415be4dc012a831a401cd46a2f3Achievement


// 实现相应多个**Confirm方式
detailConfirm: function(e) {
  var detail = e.detail.value;
}
realnameConfirm: function(e) {
  var realname = e.detail.value;
}
mobileConfirm: function(e) {
  var mobile = e.detail.value;
}

It can be seen from the comparison between method one and method two that although they can also achieve the goal of obtaining values, their usage scenarios are different. Different, the former is suitable for submitting a large number of form items, such as users completing personal information and filling in the delivery address; while the latter is suitable for only one or two form items, such as entering a courier number and binding a mobile phone number.

If you need an immediate response similar to ajax, you should choose the latter, because input can use 97657f03b0e24b396cac09fd405cebe1 to get the value instantly, such as entering mobile phone keywords in the product search box. Scenarios such as iPhone7, Mate8 and other candidate words should appear.

Document source: https://mp.weixin.qq.com/debug/wxadoc/dev/component/input.html

Summary:

List Index subscript value, page value transfer, form form value transfer, the first type is used all the time, the second type is also very commonly used, but the small program pages generally have fewer pages, my current project only has 12 pages, the third This type is relatively rarely used, because the mobile phone is not a productivity tool after all, and is used on registration pages, comment pages, etc.

The above is the detailed content of Summarize several methods of passing and obtaining values ​​in the development of WeChat mini programs. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn