【问题标题】:Get increment value from input area in a for-loop在for循环中从输入区域获取增量值
【发布时间】:2015-03-25 23:33:48
【问题描述】:

我已经完成了这项工作fiddle:

HTML

<div id="container">
    <div id="input_container">
        <label for="increment_t">Set t-increment value (default 0.1):</label>
        <input id="increment_t" type="number" min="0" max="1" step="0.1" value="0.1" />
    </div>
    <br />
    <div id="generator_container">
        <a id="downloadlink" download="outfile.txt">Download</a>
        <button id="create_link">Export output</button>
    </div>
</div>

CSS

#container {
    position: absolute;
    display: block;
    left: 0;
    top: 0;
    padding: 10px 10px; /* (top-bottom) (left-right) */
    z-index: 1;
}
#input_container {
    display: block;
    float: left;
    margin-bottom: 10px;
}
#increment_t {
    max-width: 50px;
}
#generator_container {
    display: block;
    float: right;
}
#downloadlink {
    display: none;
    margin-right: 10px;
}
#create_link {
    float: right;
}

JavaScript

(function () {
    var t_values;
    var t_values_list = [];
    for ( var t=0; t<=1; t+=0.1 ){
        t_values = t.toFixed(1);
        t_values_list.push(t_values);
    }    

    var textFile = null;
    makeTextFile = function (text) {
        var data = new Blob([text], {type: 'text/plain'});

        if (textFile !== null) {
            window.URL.revokeObjectURL(textFile);
        }

        textFile = window.URL.createObjectURL(data);

        return textFile;
    };

    var create_link = document.getElementById('create_link');

    create_link.addEventListener('click', function () {
        alert(t_values_list);
        var link = document.getElementById('downloadlink');
        link.href = makeTextFile(t_values_list.join('\n'));
        link.style.display = 'inline-block';
    }, false);
})();

但我想为“t”变量(即0.1,在for-loop中)设置增量值从输入区域。我试过this的方式:

JavaScript

(function () {
    var t_values;
    var t_values_list = [];
    alert(document.getElementById("increment_t").value); // test to get the t value, to use it in the following loop for
    for ( var t=0, increment_t=document.getElementById("increment_t").value; t<=1; t+=increment_t ){
        t_values = t.toFixed(1);
        t_values_list.push(t_values);
    }    

    var textFile = null;
    makeTextFile = function (text) {
        var data = new Blob([text], {type: 'text/plain'});

        if (textFile !== null) {
            window.URL.revokeObjectURL(textFile);
        }

        textFile = window.URL.createObjectURL(data);

        return textFile;
    };

    var create_link = document.getElementById('create_link');

    create_link.addEventListener('click', function () {
        alert(t_values_list);
        var link = document.getElementById('downloadlink');
        link.href = makeTextFile(t_values_list.join('\n'));
        link.style.display = 'inline-block';
    }, false);
})();

但它不起作用。感谢您的帮助

【问题讨论】:

    标签: javascript for-loop input increment


    【解决方案1】:

    如果您在 javascript 中从其他地方获取数字...总是解析它们。 parseInt(..., 10) 或在您的示例中:parseFloat

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-28
    • 1970-01-01
    • 2020-02-21
    • 2019-07-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多