在微信小程序中是不能修改input樣式的 甚至修改大小也不能,那麼怎麼作一個自定義樣式的input呢javascript
說一下我作的input的原理 有兩張圖片 一張是未選中的(input.png)一張是已經選中的
(input_n.png) 更具點擊事件bindtap 事件來更換圖片的路徑實現java
首先請求後臺接口獲取數據json
wx.request({ url: imgsrc + '/wechar/product/getproduct', data: '', header: {}, method: 'GET', dataType: 'json', responseType: 'text', success: function (res) { console.log(res); that.setData({ product: res.data, }); }, })
得到數據格式 小程序
把這些數據存入data裏面微信小程序
在wxml中寫循環給圖片寫入事件cli1
把數組下標存入data-id 用於區分點擊了哪一個按鈕
<view class="boxaa" wx:for="{{product}}" > <view class='gongpin'> <image src='{{imgsrc+item.pro_imgs}}'></image> <view class='descript'>{{item.pro_name}}</view> <view class='price'>{{item.pro_price}}</view> </view> <image class='radiocheck' data-proid="{{item.pro_id}}" bindtap='cli1' src='../../imgs/{{item.imgsrc}}'data-name="{{item.pro_name}}" data-id="{{index}}" ></image>
js代碼數組
cli1:function(res) {
//獲取數組的下標 用來確認點擊的是那個按鈕
var id = res.currentTarget.dataset.id;
//把選中的商品名字存起來 selectedProName = res.currentTarget.dataset.name; //把選中的商品id存起來
selectedProId = res.currentTarget.dataset.proid; //由於是單選按鈕首先循環全部的商品把input改成未選中的狀態 for (var x in product) { product[x].imgsrc = "radio.png"; }
//根據獲取過來的數組下標判斷input是不是選中狀態 若是是切換爲未選中狀態 若是不是改成選中狀態 if (product[id].imgsrc == "radio.png") { product[id].imgsrc = "radio_n.png"; } else { product[id].imgsrc = "radio.png"; }
把整個數組存入data中 this.setData({ product: product, }); }