javascript - 小程序中字符串的JSON数据如何转换为Number类型

查看:76
本文介绍了javascript - 小程序中字符串的JSON数据如何转换为Number类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

/** index.js*/
var app = getApp()  
Page({  
    data: {  
        /**   
         * 页面配置  
         */  
        winWidth: 0,  
        winHeight: 0,  
        // tab切换    
        currentTab: 0, 
                option1:[
          {value:"1",contant:"A:xxxx"},
          {value:"2",contant:"B:xxxx"},
          {value:"3",contant:"C:xxxx"},
          {value:"4",contant:"D:xxxx"}
        ],
        option2: [
          { value: "1", contant: "A:xxxx" },
          { value: "2", contant: "B:xxxx" },
          { value: "3", contant: "C:xxxx" },
          { value: "4", contant: "D:xxxx" }
        ], 
        option3: [
          { value: "1", contant: "A:xxxx" },
          { value: "2", contant: "B:xxxx" },
          { value: "3", contant: "C:xxxx" },
          { value: "4", contant: "D:xxxx" }
        ],
        option1Value:0,
        option2Value:0,
        option3Value:0,
    },  
    onLoad: function() {   swiperChange: function(e){
                var that = this;
        if(that.data.currentTab==0){
          that.setData({
            option1Value : e.detail.value,
            currentTab: 1 
          });
        }else if(that.data.currentTab==1){
          that.setData({
            option2Value : e.detail.value,
            currentTab: 2
          });
        }else if(that.data.currentTab==2){
          that.setData({
            option3Value : e.detail.value,
            currentTab: 3
          })
        } 
         }   


})  

<!-- index.wxml -->
                    <!-- 问题区域 -->
<view class="swiper-tab">    
    <view class="swiper-tab-list {{currentTab==0 ? 'on' : ''}}" data-current="0" bindtap="swichNav">第一题</view>    
    <view class="swiper-tab-list {{currentTab==1 ? 'on' : ''}}" data-current="1" bindtap="swichNav">第二题</view>    
    <view class="swiper-tab-list {{currentTab==2 ? 'on' : ''}}" data-current="2" bindtap="swichNav">第三题</view>      
</view>

                    <!-- 选项区域 -->
<swiper current="{{currentTab}}" class="swiper-box" duration="300" style="height:{{winHeight - 31}}px" bindchange="bindChange">    

                    <!-- 第一题选项 -->    
    <swiper-item>    
      <radio-group bindchange="swiperChange">
        <label class="option" wx:for="{{option1}}">
          <radio value="{{item.value}}" />{{item.contant}}
        </label>
      </radio-group>   
    </swiper-item>    

                    <!-- 第二题选项 -->    
    <swiper-item>    
      <radio-group bindchange="swiperChange">
        <label class="option" wx:for="{{option2}}">
          <radio value="{{item.value}}" />{{item.contant}}
        </label>
      </radio-group>   
    </swiper-item>  

                    <!-- 第三题选项 -->    
    <swiper-item>    
      <radio-group bindchange="swiperChange">
        <label class="option" wx:for="{{option3}}">
          <radio value="{{item.value}}" />{{item.contant}}
        </label>
      </radio-group>   
    </swiper-item> 

     <swiper-item>    
        <view>{{score}}</view>
    </swiper-item>

以上是我的代码,想实现页面中有三个题目,分别有四个不同分值的选项,用户选择后直接弹出结果,但是储存用户选择的选项的JSON数据是字符串,请问如何转换为数字类型?

这个问题已被关闭,原因:问题质量差 - 问题太水、伸手党

解决方案

option1Value: parseInt(e.detail.value),

这篇关于javascript - 小程序中字符串的JSON数据如何转换为Number类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆