一个微信小程序开发项目,在整个开发流程中,必要用到的就是参数取值,一般来说,常见的参数取值有列表index下标取值与form表单取值。下面分别来介绍一下这两种取值各有什么方法去实现。
1、列表index下标取值
(1)实现方式:data-index="{{index}}"及e.currentTarget.dataset.index。
(2)生成值
<image src="../../../images/icon_delete.png" /><text>删除</text>
删除图标与文字添加data-index="{{index}}"自定义属性以及绑定点击事件bindtap="delete"。示例代码如下:
<view data-index="{{index}}" bindtap="delete"><image src="../../../images/icon_delete.png" /><text>删除</text></view>
自定义属性与绑定点击事件之后,实现delete方法,取到index下标值,代码如下:
delete: function (e) {
var index = parseInt(e.currentTarget.dataset.index);
console.log("index" + index);
}
(3)取出值
从index数据中找出相应元素并删除地址。示例代码如下。
// 找到当前地址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、form表单取值
(1)方式一:<form bindsubmit="formSubmit">与<button formType="submit">标签配合使用。
<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>
<form bindsubmit="formSubmit">与<button formType="submit">标签配合使用后,用js取值。
formSubmit: function(e) {
// detail
var detail = e.detail.value.detail;
// realname
var realname = e.detail.value.realname;
// mobile
var mobile = e.detail.value.mobile;
}
(2)方式二:通过<input bindconfirm="realnameConfirm">实现form表单取值
// 实现相应多个**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;
}