- 当前 Bug 的表现(可附上截图)
- 预期表现
- 复现路径
- 提供一个最简复现 Demo
Page({
data: {
str:'A,B',
s:'A',
t:'C'
},
})
<view style="margin:4px;"><checkbox value="{{s}}" checked="{{str.indexof(s)!=-1?true:false}}"/>{{str}}是否包含{{s}}</view>
<view style="margin:4px;"><checkbox value="{{s}}" checked="{{str.indexof(s)>=0?true:false}}"/>{{str}}是否包含{{s}}</view>
<view style="margin:4px;"><checkbox value="{{t}}" checked="{{str.indexof(t)!=-1?true:false}}"/>{{str}}是否包含{{t}}</view>
多选框想显示已选择,这几种都不行。
indexOf(String s)的使用,如果包含,返回的值是包含该子字符串在父类字符串中起始位置;
如果不包含必定全部返回值为-1
1 package my_automation; 2 3 public class z_test { 4 5 public static void main(String[] args) { 6 String test = "This is test for string"; 7 System.out.println(test.indexOf("This")); //0 8 System.out.println(test.indexOf("is")); //2 9 System.out.println(test.indexOf("test")); //8 10 System.out.println(test.indexOf("for")); //13 11 System.out.println(test.indexOf("for string "));//-1 12 13 if (test.indexOf("This")!=-1){ 14 //"只要test.indexOf('This')返回的值不是-1说明test字符串中包含字符串'This',相反如果包含返回的值必定是-1" 15 System.out.println("存在包含关系,因为返回的值不等于-1"); 16 17 }else{ 18 19 System.out.println("不存在包含关系,因为返回的值等于-1"); 20 } 21 22 23 if (test.indexOf("this")!=-1){ 24 //"只要test.indexOf('this')返回的值不是-1说明test字符串中包含字符串'this',相反如果包含返回的值必定是-1" 25 System.out.println("存在包含关系,因为返回的值不等于-1"); 26 27 }else{ 28 29 System.out.println("不存在包含关系,因为返回的值等于-1"); 30 } 31 32 33 } 34 35 }
<wxs module="helper">
module.exports.includes = function(st, aa){
return st.indexOf(aa) > -1
}
</wxs>
这个代码很不错 总算解决问题了 感谢分享 谢谢
你好,可以了解一下wxs:https://developers.weixin.qq.com/miniprogram/dev/framework/view/wxs/
https://developers.weixin.qq.com/s/adUmXvmw766M
还是不行,请教
<wxs module="helper">
function includes(st, aa){
return st.indexOf(aa)
}
</wxs>
<view style="margin:4px;"><checkbox value="{{s}}" checked="{{helper.includes(str,s)>=0?true:false}}"/>{{str}}是否包含{{s}}{{helper.includes(str,s)}}</view>
<view style="margin:4px;"><checkbox value="{{s}}" checked="{{helper.includes(str,s)}}"/>{{str}}是否包含{{s}}</view>
<view style="margin:4px;"><checkbox value="{{t}}" checked="{{helper.includes(str,s)}}"/>{{str}}是否包含{{t}}{{helper.includes(str,t)}}</view>
<wxs module="helper">
module.exports.includes = function(st, aa){
return st.indexOf(aa) > -1
}
</wxs>
可以了,非常感谢!
建议在js里做好操作后 显示数据到页面上。