html 事件参数值,【已解决】小程序中给点击事件函数传递参数或从html元素中获取值...

现在希望对于小程序:

dc40959782297872386efea25c9f5879.png

列表中每行的整个区域,点击后,可以获取对应的此处该book的id

之前html中做法是html中加上个hidden的id,book的click的item中获取对应id

现在要去看看小程序中如何支持

先去加上点击事件

小程序点击事件传参

此处只有单一参数,所以暂时去用id试试

可行之后,再去换用dataset

此处空的callback函数中也可以看到:

currentTarget中的dataset和id

a2c7f449fd222b17dbd04a9e52c92626.png

先去加上id试试:

id="{{curBookItem.id}}"

wx:for="{{searchBookList}}"

wx:for-index="bookIdx"

wx:for-item="curBookItem"

wx:key="id"

class='book_list_item'

bindtap='bookItemClickCallback'

>

e63429526f2f92070cd2e26c09fafb45.png

但是在加id的时候,担心:

此时处于在for循环中:

wx:for-item=”curBookItem”

刚刚指定的curBookItem

在当前view中能用

curBookItem.id

吗?

去试试再说

是可以的:

b673c1daa2c37b5e9f792c32ec4b17ff.png

再去看看click的event中,能否获取到id值:

是可以获取到的:

f5c1b770b4aeade05a3008d33583ebf4.png

然后用:var curBookId = e.currentTarget.dataset.id

var curBookId = e.currentTarget.dataset["id"]

都是:undefined

参考:

换用:var curBookId = e.currentTarget.id

相关代码:bookItemClickCallback: function(e){

console.log("bookItemClickCallback: e=%o", e)

//redirect to detail page

// var curBookId = e.currentTarget.dataset.id

// var curBookId = e.currentTarget.dataset["id"] // undefined

var curBookId = e.currentTarget.id // 5bd7bf97bfaa44fe2c7415d9

console.log("curBookId=%s", curBookId)

},

才获取到值:

c6065ccc27d90110409ef10a57e417c7.png

后来从log中发现是自己之前看错了:

4701ac1b20d11c072b0683383e62641e.png

不是dataset中的id,而是:currentTarget下面的id

其他参数可以通过:

去传递到:

e.currentTarget.dataset

随便去试试:bookItemClickCallback: function(e){

console.log("bookItemClickCallback: e=%o", e)

//redirect to detail page

// var curBookId = e.currentTarget.dataset.id

// var curBookId = e.currentTarget.dataset["id"] // undefined

var curBookId = e.currentTarget.id // 5bd7bf97bfaa44fe2c7415d9

console.log("curBookId=%s", curBookId)

var difficulty = e.currentTarget.dataset.difficulty

// var difficulty = e.currentTarget.dataset["difficulty"]

console.log("difficulty=%s", difficulty)

},

和:

id="{{curBookItem.id}}"

wx:for="{{searchBookList}}"

wx:for-index="bookIdx"

wx:for-item="curBookItem"

wx:key="id"

class='book_list_item'

bindtap='bookItemClickCallback'

data-difficulty="{{curBookItem.grading.difficulty}}"

>

效果:

8f72a5b9aea46228010e3b79212d0c1f.png

【总结】

对于点击事件:普通html中叫做:click

on click

小程序中叫做:tap

bindtap

在小程序中挂载了点击事件的callback函数:

index.wxml

id="{{curBookItem.id}}"

wx:for="{{searchBookList}}"

wx:for-index="bookIdx"

wx:for-item="curBookItem"

wx:key="id"

class='book_list_item'

bindtap='bookItemClickCallback'

data-difficulty="{{curBookItem.grading.difficulty}}"

>

以及对应的js中的函数:

index.jsbookItemClickCallback: function(e){

console.log("bookItemClickCallback: e=%o", e)

//redirect to detail page

var curBookId = e.currentTarget.id // 5bd7bf97bfaa44fe2c7415d9

console.log("curBookId=%s", curBookId)

var difficulty = e.currentTarget.dataset.difficulty

// var difficulty = e.currentTarget.dataset["difficulty"]

console.log("difficulty=%s", difficulty)

},

即可获取到:单个参数且是id:

对于普通的,单个要传递的值,往往是id,这时候就可以wxml中:id=”{{yourIdValue}}”

js中:e.currentTarget.id

而获取到id值

多个其他参数:

对于更多的,其他的(多个)参数,可以通过dataset:

wxml中:data-otherVariableName=”{{otherVariableValue}}”

js中:

e.currentTarget.dataset.otherVariableName

或:

e.currentTarget.dataset[“otherVariableName”]

而获取到otherVariableName的值:otherVariableValue

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值