微信小程序之间的参数传递、获取的操作方法
作者:25号底片~
这篇文章主要介绍了微信小程序中如何获取和传递参数,包括获取当前页面参数、单独input文本框参数的获取、表单获取参数信息、点击表格单元格信息获取行ID以及前端页面跳转传递多个参数等,感兴趣的朋友跟随小编一起看看吧
一、获取当前页面参数
js
onLoad: async function (options) { var pages = getCurrentPages() //获取加载的页面 var currentPage = pages[pages.length - 1] //获取当前页面的对象 var options = currentPage.options //如果要获取url中所带的参数可以查看options this.setData({ contactid: options.contactid//这里的options.表示获取参数,contactid表示参数名称 }) },
当前页面参数可以在小程序开发工具的右下角查看
二、单独input文本框参数的获取
wxml:这里的bindconfirm指的是回车事件,也可以使用别的事件
<input class="input_comments" type="text" bindconfirm="comments" placeholder="请输入内容"/>
js
comments(e) { console.log("参数",e.detail.value) //这里面的值就是获取到文本框在中的值 },
输出结果:在文本框输入:这是一个测试,回车,得到的结果如下
三、表单获取参数信息(包括多选,下拉列表,文本框,文本域)
案例
wxml
<form action="" bindsubmit="addcontent_submit"> <view class="pop1" bindtap="customerselect"> <view class="pop1_title"> <text style="color:red;">*</text> <text>客户</text> </view> <view style=" display: flex;"> <input class="pop1_input" disabled="true" placeholder="请选择" name="customername" /> <view style="color:#808080;text-align:right;padding-right:5px;;width:15%">></view> </view> <view class="customered bright789_view_hide{{showView?'bright789_view_show':''}}"> <view class="customered1"> <view class="customered_title">客户名称</view> <view class="customered_content" >{{valid.customer_name}}</view> </view> </view> </view> <view class="pop2"> <view class="pop2_title"> <text style="color:red;">*</text> <text>姓名</text> </view> <input class="pop2_input" placeholder="请输入" name="contactname" /> </view> <view class="pop3"> <view class="pop3_title"> <text>手机号</text> </view> <input class="pop3_input" placeholder="请输入" name="contactphone" /> </view> <view class="pop4"> <view class="pop4_title"> <text>职位</text> </view> <view class="pop4_checkbox"> <checkbox-group bindchange="handleItemChange"> <checkbox value="{{item.jobname}}" wx:for="{{list}}" wx:key="id"> {{item.jobname}} </checkbox> </checkbox-group> <view>选中职位:{{checkedList}}</view> </view> </view> <view class="pop5" bindtap="policymaker"> <view class="pop5_title"> <text>是否为关键决策人</text> </view> <picker name="policymaker" bindchange="bindPickerChange6" value="{{index6}}" range="{{selectDatas6}}"> <view class="pop5_input" style="float:left" name="policymaker"> {{seleNull6?selectDatas6[index6]:'请选择'}} </view> <view style="font-size:18px;color:#808080;float:right;width:20px;">></view> </picker> </view> <view class="pop6"> <view class="pop6_title"> <text>邮箱</text> </view> <input class="pop6_input" name="contactemail" placeholder="请输入邮箱" /> </view> <view class="pop7"> <view class="pop7_title"> <text>备注</text> </view> <textarea bindblur="bindTextAreaBlur1" class="pop7_input" name="contactremarks" placeholder="请输入"></textarea> </view> <view class="pop8"> <view style="padding-top:2%;"> <button style="background-color:#70bdac;width:90%;" form-type="submit">提交</button></view> </view> </form>
wxss
.pop1 { background-color:white; width: 100%; padding-top:2%; margin-bottom:2%; } /* 点击事件执行样式变化 */ .pop1:active{ background-color:rgb(212, 211, 211); } .pop1_title { margin-left:5%; } .pop1_input{ margin-left:7%; width:85%; color:#808080; } .customered{ width:100%; padding-bottom:5%; display: flex; align-items: center; justify-content: center; } .bright789_view_hide{ display: none; } .bright789_view_show{ display: block; } .customered1{ border:1px solid #EDEDEE; width:85%; } .customered_title{ color:#808080; margin:3% 0 0 2%; } .customered_content{ word-break: break-all; padding-left:2%; margin:0 0 3% 2%; } .pop2{ background-color:white; width: 100%; padding-top:2%; height:8%; margin-bottom:2%; } .pop2_title{ margin-left:5%; } .pop2_input{ margin-left:7%; } .pop3{ background-color:white; width: 100%; padding-top:2%; height:8%; margin-bottom:2%; } .pop3_title{ margin-left:7%; } .pop3_input{ margin-left:7%; } .pop4{ background-color:white; width: 100%; height:20%; margin-bottom:2%; } .pop4_title{ margin-left:7%; padding-top:2%; padding-bottom:2%; } .pop4_checkbox{ margin-left:7%; color:#808080; width:90%; } .pop4_checkbox checkbox{ margin-left:2%; margin-bottom:2%; } .pop5{ background-color:white; width: 100%; margin-bottom:2%; height:9%; } .pop5_title{ margin-left:7%; padding-top:2%; } /* 点击事件执行样式变化 */ .pop5:active{ background-color:rgb(212, 211, 211); } .pop5_input{ margin-left:7%; width:85%; color:#808080; } .pop6{ background-color:white; width: 100%; height:9%; margin-bottom:2%; } .pop6_title{ margin-left:7%; padding-top:2%; } .pop6_input{ margin-left:7%; } .pop7{ background-color:white; width: 100%; margin-bottom:4%; } .pop7_title{ margin-left:7%; padding-top:2%; } .pop7_input{ margin-left:7%; } .pop8{ height:10%; background-color:white; width: 100%; bottom:0px; } .pop8 button{ color:white; }
js
const app = getApp() Page({ data: { index6: 0, //选择的下拉列 表下标, seleNull6: null, //设置的变量 contactremarks: '', valid: '', //客户选择页面传来的值 showView: false, selectDatas6: ['', '是', '否'],//从前端获取下拉列表的值 customerinfo: '', checkedList: [], }, //职位复选框 handleItemChange(e) { console.log(e.detail.value); // 1 事件触发时选中复选框的值 const checkedList = e.detail.value; // 2 进行赋值 this.setData({ checkedList }) }, //是否为决策人 bindPickerChange6: function (e) { console.log('picker发送选择改变,携带下标为', e.detail.value) console.log('picker发送选择改变,携带值为' + this.data.selectDatas6[e.detail.value]) this.setData({ seleNull6: '0', index6: e.detail.value }) }, //选择客户 customerselect() { wx.navigateTo({ url: '/pages/customerselect/customerselect', }) }, //新增联系人 addcontent_submit(e) { //获取输入值(获取前端的参数) this.setData({ customer_id: this.data.valid.customer_id, //客户 customer_code: this.data.valid.customer_code, //客户 contactname: e.detail.value.contactname, //联系人姓名 contactphone: e.detail.value.contactphone, //联系人电话 checkedList: this.data.checkedList, //职位 policymaker: this.data.selectDatas6[e.detail.value.policymaker], //决策人 contactemail: e.detail.value.contactemail, //邮箱 contactremarks: e.detail.value.contactremarks //备注 }) //定义输入的值 let customer_id = this.data.customer_id let customer_code = this.data.customer_code let contactname = this.data.contactname let contactphone = this.data.contactphone let checkedList = this.data.checkedList let policymaker = this.data.policymaker let contactemail = this.data.contactemail let contactremarks = this.data.contactremarks console.log('客户id', customer_id) console.log('客户', customer_code) console.log('名称', contactname) console.log('电话', contactphone) console.log('职位', checkedList) console.log('决策人', policymaker) console.log('邮箱', contactemail) console.log('备注', contactremarks) if (this.data.valid.customer_code == "" || this.data.valid.customer_code == null || this.data.valid.customer_code == undefined) { wx.showToast({ title: '请选择客户', icon: 'none' }) return } else if (contactname.length == 0) { wx.showToast({ title: '请输入姓名', icon: 'none' }) return } else { console.log(app.globalData.username) //添加数据 wx.request({ url: app.globalData.position + 'Crm/addcontact', data: { customer_id: customer_id, customer_code: customer_code, contactname: contactname, contactphone: contactphone, checkedList: checkedList, policymaker: policymaker, contactemail: contactemail, contactremarks: contactremarks, username: app.globalData.username }, header: { // 'content-type': 'application/json' // 默认值(固定,我开发过程中还没有遇到需要修改header的) "Content-Type": "application/x-www-form-urlencoded" }, method: 'POST', dataType: 'json', success: res => { wx.showToast({ title: '新建成功', duration: 1000 //持续的时间 }) var pages = getCurrentPages(); //获取当前页面信息 var prevPage = pages[pages.length - 2]; //上一个页面 prevPage.onLoad(); //触发上一个页面的onLoad生命周期函数(相当于刷新上一个页面) wx.navigateBack({ //返回上一个页面 delta: 1 }) }, fail(res) { console.log("查询失败") } }) } }, //进入页面显示 onLoad: async function (options) { //查询所有职务,从服务器查询职务信息 wx.request({ url: app.globalData.position + 'Crm/customejob_select', data: {}, header: { // 'content-type': 'application/json' // 默认值(固定,我开发过程中还没有遇到需要修改header的) "Content-Type": "application/x-www-form-urlencoded" }, method: 'POST', dataType: 'json', success: res => { // console.log("职务", res.data) this.setData({ list: res.data }) }, fail(res) { console.log("查询失败") } }) }, onShow(options) { console.log(this.data.valid) //获取到选择页面的数据 if (this.data.valid == "" || this.data.valid == null || this.data.valid == undefined) { // console.log("为空"); this.setData({ showView: false }) } else { // console.log("不为空"); this.setData({ showView: true }) } }, //分享 onShareAppMessage() { wx.removeStorageSync('username'); return { title: 'crm系统', path: '/pages/login/login', imageUrl: '/images/title/title.jpg', } }, //刷新 onPullDownRefresh() { this.onLoad(); //需要再次调用接口的函数 setTimeout(function () { // 不加这个方法真机下拉会一直处于刷新状态,无法复位 wx.stopPullDownRefresh() }, 3000) }, })
thinkphp(后端)
//新增联系人 public function addcontact() { $time = time(); //获取当前时间戳 $customer_code = input('post.customer_code'); //客户代号 $contactname = input('post.contactname'); //姓名 $contactphone = input('post.contactphone'); //电话 $checkedList = input('post.checkedList'); //职位 $policymaker = input('post.policymaker'); //决策人 $contactemail = input('post.contactemail'); //邮箱 $contactremarks = input('post.contactremarks'); //备注 $username = input('post.username'); //负责人 $business_code = DB::table('fa_crm_user')->where(['username' => $username])->value('business_code'); $data = [ 'customer_code' => $customer_code, 'contacts_name' => $contactname, 'contacts_phone' => $contactphone, 'contacts_position' => $checkedList, 'key_decision_makers' => $policymaker, 'contacts_email' => $contactemail, 'contacts_remark' => $contactremarks, 'customer_principal' => $business_code, 'creation_date' => $time, 'enable_flag' => 'Y' ]; db::table('customer_contact_xcx')->insert($data);//新增 //获取联系人数据库中的id $maxid = DB::table('customer_contact_xcx')->max('id'); //向客户动态添加数据 $data2 = [ 'customer_code' => $customer_code, 'operation' => '新增联系人', 'details_id' => $maxid, 'creation_date' => $time, 'customer_principal' => $business_code, ]; db::table('schedule_flow')->insert($data2); } //查询所有职务 public function customejob_select() { $customer_job = DB::table('customer_job')->select(); echo json_encode($customer_job); }
四、点击表格单元格信息,获取改行id
样式:点击报价单号,跳转到详情页
wxml:
<view style="padding:15px;"> <view class="table"> <view class="table-tr"> <view class="table-th1">报价单号</view> <view class="table-th2">客户简称</view> <view class="table-th3">客户名称</view> <view class="table-th4">金额</view> <view class="table-th4">需求日期</view> <view class="table-th4">报价日期</view> <view class="table-th4">交货日期</view> </view> <view class="table-tr" wx:for="{{pricelist_item}}" wx:key="unique"> <view class="table-td1" bindtap="ppricelist_update" data-id="{{item.id}}">{{item.pricelist_num}}</view> <view class="table-td2">{{item.customer_code}}</view> <view class="table-td2">{{item.customer_name}}</view> <view class="table-td2">{{item.amount}}</view> <view class="table-td2">{{item.need_time}}</view> <view class="table-td2">{{item.offer_time}}</view> <view class="table-td2">{{item.delivery_time}}</view> </view> </view> </view>
在报价单的td中,设置方法bindtap="ppricelist_update"
设置需要传递的参数信息data-id="{{item.id}}"
js:执行方法跳转到另外一个页面
//点击报价单号进入详情页 ppricelist_update(e){ // console.log(e.currentTarget.dataset.id)//获取到报价单页面的参数 wx.navigateTo({ //需要修改为wx.navigateTo //跳转到新增页面,不销毁原页面 url: '/pages/price_list_update/price_list_update?pricelist_id=' + e.currentTarget.dataset.id, }) },
e.currentTarget.dataset.id:为前端设置的data-id的数据
?后面的数据即为参数信息pricelist_id=' + e.currentTarget.dataset.id
在新页面获取数据
js:
//进入页面 onLoad: async function (options) { var pages = getCurrentPages() //获取加载的页面 var currentPage = pages[pages.length - 1] //获取当前页面的对象 var options = currentPage.options //如果要获取url中所带的参数可以查看options // console.log(options.pricelist_id);//控制台输出页面参数 },
五、前端页面跳转,传递多个参数
follow_up(e) { wx.navigateTo({//跳转到新增页面,不销毁原页面 url: '/pages/new_followrecord/new_followrecord?customer_code=' + e.currentTarget.dataset.id + "&issent=" + 1 + "&nowDate=" + this.data.nowDate, }) },
如上图:有参数customer_code,issent,nowDate
?后面加参数,参数与参数之间&连接
到此这篇关于微信小程序之间的参数传递、获取的文章就介绍到这了,更多相关微信小程序参数传递获取内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!