search
HomeWeChat AppletMini Program DevelopmentSummary of value transfer methods for WeChat mini programs

Summary of value transfer methods for WeChat mini programs

Sep 06, 2017 am 10:49 AM
AppletsSummarizemethod

微信小程序 传值取值
小程序里常见的取值有以下几种,一个完整的项目写下来,用到的概率几乎是100%。
列表index下标取值
页面传值
form表单取值
1. 列表index下标取值
实现方式是:data-index="pw_index"挖坑及e.currentTarget.dataset.index来填坑即可
1.1 生成值

<image src="../../../images/icon_delete.png" /><text>删除</text>


在删除图标与文字添加data-index="pw_index"自定义属性以及绑定点击事件bindtap="delete"

<image src="../../../images/icon_delete.png" /><text>删除</text>


实现delete方法,取到index下标值。


图片:1.png

Summary of value transfer methods for WeChat mini programs

delete: function (e) {
  var index = parseInt(e.currentTarget.dataset.index);
  console.log("index" + index);
}


如果不使用e.currentTarget而使用e.target会怎样?
将会导致仅点中才能输出index值,点子元素将输出NaN。


图片:2.png

Summary of value transfer methods for WeChat mini programs

那target有什么用呢,用于区分子元素与外部元素要分别处理时,比如换用户头像的场景,点击头像本身预览大图,而头像所在的点整一行,将是切换头像。


图片:3.png

Summary of value transfer methods for WeChat mini programs

关于二者区别的详情说明,请见文档:https://mp.weixin.qq.com/debug/wxadoc/dev/framework/view/wxml/event.html
1.2 取出值
试图从index数据中找出相应元素删除地址

// 找到当前地址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. 页面传值
从收货地址列表页中传地址id到编辑页面,以读取原地址供修改之用。
address/list页面实现以下代码

<view class="container" data-index="pw_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
  });
},
address/add页面实现onLoad(options)方法,从url路径中获取objectId
onLoad: function (options) {
  var objectId = options.objectId
}


然后就是访问网络以及渲染页面了。


图片:4.png

Summary of value transfer methods for WeChat mini programs

3. form表单取值
3.1 方式一,通过

The above is the detailed content of Summary of value transfer methods for 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.