JQuery UI滑块在0时不起作用?

时间:2017-09-02 04:02:28

标签: javascript jquery jquery-ui meteor

我正在尝试使用JQuery UI在我的meteor应用程序中创建一个滑块,其中可能的值介于0和1之间,步长为0.001,我不希望滑块具有默认值,也不显示滑块的手柄直到用户首先决定一个值。

Template.game_task.onCreated(()=>{
    if (!Session.get("sliderValue")){
        Session.setPersistent('sliderValue', -1)
    }

});

Template.game_task.onRendered(()=> {

    /////// the slider stuff //////
    this.$("#slider").slider({
        min: -0.0000,
        max: 1,
        step: 0.01,
        slide: function(event, ui) {
            $('#slider > .ui-slider-handle').show();
            Session.setPersistent('sliderValue',ui.value)
        },
        change: function(event, ui) {
            //save value to collection
        }
    });

    if (Session.get("sliderValue") === -1){
        //if no value is set, then hide the handle
        $('#slider > .ui-slider-handle').hide();
    }

});

Template.game_task.helpers({
    getSliderValue(){
        const sliderValue = Session.get('sliderValue');
        if (sliderValue){
            console.log('slider value to be returned',sliderValue)
            return sliderValue;
        }
    },

});

这是HTML:

<div class='row'>
    <div id="slider" class="custom-handle"></div>
    <span id="min">0</span>
    <span class="quarter">0.25</span>
    <span class="quarter">0.5</span>
    <span class="quarter">0.75</span>
    <span id="max">1</span>
    <p class="contributionText">Your guess: {{getSliderValue}}</p>
</div>

但是,滑块在下限处给出了奇怪的行为(当我将滑块拉向0时)。当我转到0时,我得到小值(即0.01,0.016),然后我得到的值为null(根据getSliderValue()中的{{getSliderValue}}函数。)

enter image description here

所以我永远无法选择0 ..但是当我进入1时,这不会发生在高端。

enter image description here

另外,如果我将最小值更改为0以外的值,我会在下限获得正确的行为(但在我的情况下,它必须为0)。

1 个答案:

答案 0 :(得分:2)

您的问题出在以下代码中:

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <stdlib.h>
// Declare constants

// Name of file that stores our raw data
#define FILE_NAME "data_1.csv"

// Data size
#define MAX_ROWS 20
#define MAX_COLUMNS 20

// Main entry point for the program
int main(void) {
    // Decalred variables
    int rowIndex = 0;
    int columnIndex = 0;
    double rawData[MAX_ROWS][MAX_COLUMNS]; // 2-dimensional array to store our 
    raw data
    // Misc variables used for reading the data from the file
    double tempfloat = 0.0;
    double tmp = 0.0;
    char newline;

    // ----------------------------------------------------------------------
    // Open the file for reading 
    FILE *infp;
    infp = fopen(FILE_NAME, "r");

    // Check for errors and exit if found
    if (infp == NULL) {
        printf("Error: failed to open %s for reading\n", FILE_NAME);
        return(1);
    }

    // Read the file into the data structure
    for (rowIndex = 0; rowIndex < MAX_ROWS; rowIndex++) {
        // Read up until the last value
        for (columnIndex = 0; columnIndex < MAX_COLUMNS - 1; columnIndex++) {
            if (fscanf_s(infp, "%lf,", &tempfloat) == 1) {
                rawData[rowIndex][columnIndex] = tempfloat;
            } else {
                printf("Error: incorrect file format at row %d, col %d.\n", rowIndex + 1, columnIndex + 1);
                return 1;
            }
        }

        // Read the last value and the newline char
        if (fscanf_s(infp, "%lf%c", &tempfloat, &newline) == 2) {
            // Check if the last character in the line was a \n otherwise an error occured.
            //Xiao: I have added newline != '\n'
            if (newline != '\r' && newline != '\n') {
                printf("Error: incorrect file format at line %d. did not find a newline.\n", rowIndex + 1);
                return 1;
            } else {
                rawData[rowIndex][columnIndex] = tempfloat;
            }
        }
    }
    // ----------------------------------------------------------------------
    // Print out the rawdata array
    printf(" --- RAW DATA ---\n");
    for (rowIndex = 0; rowIndex < MAX_ROWS; rowIndex++) {
        // Read up until the last value
        for (columnIndex = 0; columnIndex < MAX_COLUMNS; columnIndex++) {
            printf("%.9f ", rawData[rowIndex][columnIndex]);
        }
        printf("\n");
    }

    // Exit
    return 0;
}

由于您的值为if (sliderValue){ ... } ,因此此条件不会为0。您应该将其更改为:

true