WXML代码如下
<van-tag type="danger" size="large" Position="center">请选取猪的身长(cm)</van-tag>
<input bindchange="getlength" class='input-radius' type="text" placeholder="请在此输入身长数据" placeholder-class="phcolor" />
<van-divider />
<van-tag type="danger" size="large" Position="center">请选取猪的体重(KG)</van-tag>
<input bindchange="getweight" class='input-radius' type="text" placeholder="请在此输入体重数据" placeholder-class="phcolor" />
<van-divider />
<button bindtap="baogu" bindtap="dadou" bindtap="xiaomai" bindtap="dami" bindtap="xsd" bindtap="yan" >点击进行饲料配比(单位均为克)</button>
<text>苞谷(玉米)需要量:{{baoguweb}}</text>
<van-divider />
<text>小麦需要量:{{xiaomaiweb}}</text>
<van-divider />
<text>大米(稻米)需要量:{{damiweb}}</text>
<van-divider />
<text>小苏打(碳酸氢钠)需要量:{{xsdweb}}</text>
<van-divider />
<text>盐需要量:{{yanweb}}</text>
<van-divider />
JS代码如下:
var length,weight,baogu,dadou,xiaomai,dami,xsd,yan
Page({
data: {
baoguweb:"",
dadouweb:"",
xiaomaiweb:"",
damiweb:"",
xsdweb:"",
yanweb:""
},
getlength:function(event){
length=event.detail.value
console.log(length)
},
getweight:function(event){
weight=event.detail.value
console.log(weight)
},
baogu,dadou,xiaomai,dami,xsd,yan:function(event){
if (length<70) {
baogu=Math.round(weight*1000*(0.5)*(0.2))
dadou=Math.round(0)
xiaomai=Math.round(weight*1000*(0.5)*(0.0225))
dami=Math.round(weight*1000*(0.5)*(0.15))
xsd=Math.round(weight*1000*(0.5)*(0.005))
yan=Math.round(weight*1000*(0.5)*(0.0025))
console.log(baogu)
console.log(dadou)
console.log(xiaomai)
console.log(dami)
console.log(xsd)
console.log(yan)
console.log(length)
}
else if (length>70&&length<120) {
baogu=Math.round(weight*1000*(0.4)*(0.175))
dadou=Math.round(0)
xiaomai=Math.round(weight*1000*(0.4)*(0.0525))
dami=Math.round(weight*1000*(0.4)*(0.125))
xsd=Math.round(weight*1000*(0.4)*(0.005))
yan=Math.round(weight*1000*(0.4)*(0.0025))
console.log(baogu)
console.log(dadou)
console.log(xiaomai)
console.log(dami)
console.log(xsd)
console.log(yan)
}
else if(length>120){
baogu=Math.round(weight*1000*(0.35)*(0.15))
dadou=Math.round(0)
xiaomai=Math.round(weight*1000*(0.35)*(0.04))
dami=Math.round(weight*1000*(0.35)*(0.10))
xsd=Math.round(weight*1000*(0.35)*(0.005))
yan=Math.round(weight*1000*(0.35)*(0.0025))
console.log(baogu)
console.log(dadou)
console.log(xiaomai)
console.log(dami)
console.log(xsd)
console.log(yan)
console.log(length)
}
this.setData({
baoguweb:baogu,
dadouweb:dadou,
xiaomaiweb:xiaomai,
damiweb:dami,
xsdweb:xsd,
yanweb:yan,
})
},
})
你试一下吧length填写值会不会报错
我猜测,是因为你的赋值数据并没有定义,所以报了这么一堆,但是应该不会影响使用