如图,给右边3个话筒按钮添加点击事件,之前是给每个按钮分别添加点击事件可以,但是目前想用事件委托的方式添加,如何实现?
代码如下:
<form action="" id="myform">
<div class="box">
<div class="num">
<label for="">保单号</label>
<input type="text" placeholder="请填写保单号"/>
</div>
<div class="num-btn">
<span class="icon-one iconfont icon-qy-yy-h"></span>
</div>
</div>
<div class="box">
<div class="idcard">
<label for="">身份证</label>
<input type="text" placeholder="请输入身份证号" autocomplete="off"/>
</div>
<div class="num-btn">
<span class="icon-two iconfont icon-qy-yy-h"></span>
</div>
</div>
<div class="box">
<div class="bcard">
<label for="">银行卡</label>
<input type="text" placeholder="请填写银行卡号"/>
</div>
<div class="num-btn">
<span class="icon-three iconfont icon-qy-yy-h"></span>
</div>
</div>
</form>
jquery代码如下
$(".icon-one").on("touchstart",function(){
layer.open({
title:'请说出保单号',
shadeClose: false,
style: 'width:5rem;border:none;background:linear-gradient(to right bottom,#f14f63,#7889fb);color:#fff;',
content:$("#saying").html(),
btn:['确认'],
yes:function(index){
layer.close(index)
},
})
});
$(".icon-two").on("touchstart",function(){
layer.open({
title:'请说出身份证号',
shadeClose: false,
style: 'width:5rem;border:none;background:linear-gradient(to right bottom,#f14f63,#7889fb);color:#fff;',
content:$("#saying").html(),
btn:'确认提交',
yes:function(index){
layer.close(index)
},
})
});
事件委托方式,给父元素添加点击事件,因为要弹出不同的layer内容,所以if做判断,但是不对,求解?
$(".num-btn").on("touchstart","span",function(){
if($(".icon-one")){
layer.open({
title:'请说出保单号',
shadeClose: false,
style: 'width:5rem;border:none;background:linear-gradient(to right bottom,#f14f63,#7889fb);color:#fff;',
content:$("#saying").html(),
btn:['确认'],
yes:function(index){
layer.close(index)
},
})
}else if($(".icon-two")){
layer.open({
title:'请说出身份证号',
shadeClose: false,
style: 'width:5rem;border:none;background:linear-gradient(to right bottom,#f14f63,#7889fb);color:#fff;',
content:$("#saying").html(),
btn:'确认提交',
yes:function(index){
layer.close(index)
},
})
}
})