收藏
回答

请教大家在WXML如何判断字符串包含哪个字符?

框架类型 问题类型 操作系统 工具版本
小程序 Bug Windows 2.6.0

- 当前 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>


多选框想显示已选择,这几种都不行。


回答关注问题邀请回答
收藏

4 个回答

  • 禾店短剧系统
    禾店短剧系统
    2021-06-15

    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 }
    



    2021-06-15
    有用 1
    回复
  • 岁月情长
    岁月情长
    2019-09-10

    <wxs module="helper">

    module.exports.includes = function(st, aa){

        return st.indexOf(aa) > -1

    }

    </wxs>

    这个代码很不错 总算解决问题了  感谢分享  谢谢



    2019-09-10
    有用
    回复
  • 灵芝
    灵芝
    2019-02-28

    你好,可以了解一下wxs:https://developers.weixin.qq.com/miniprogram/dev/framework/view/wxs/

    2019-02-28
    有用
    回复 4
    • 2019-02-28

      https://developers.weixin.qq.com/s/adUmXvmw766M

      还是不行,请教

      2019-02-28
      回复
    • 2019-02-28

      <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>


      2019-02-28
      1
      回复
    • Maverick
      Maverick
      2019-02-28回复

      <wxs module="helper">

      module.exports.includes = function(st, aa){

          return st.indexOf(aa) > -1

      }

      </wxs>


      2019-02-28
      5
      回复
    • 2019-02-28回复Maverick

      可以了,非常感谢!

      2019-02-28
      回复
  • 睡前原谅一切
    睡前原谅一切
    2019-02-28

    建议在js里做好操作后 显示数据到页面上。

    2019-02-28
    有用
    回复
登录 后发表内容