简体   繁体   English

用户输入为javascript变量

[英]user input as javascript variable

I have written a small function in javascript to get user input from a text input form to be used as the delay time for fading away but it fails please help 我在javascript中编写了一个小函数,可从文本输入表单获取用户输入,以用作淡出的延迟时间,但失败了,请帮忙

<input type="text" class="form-control" id="new" placeholder="Fade Duration(milliseconds)"/>

var x = document.getElementById("new").value;
$(".fadeInbox").click(function () {
    $("#section1").delay(x).fadeIn();
    $("#section2").delay(x).fadeIn();
});



$(".fadeOutbox").click(function () {
    $("#section1").delay(x).fadeOut();
    $("#section2").delay(x).fadeOut();

});

var x from input is a string not a int , so input中的var xstring而不是int ,因此

var x = parseInt(document.getElementById("new").value);
...

or 要么

var x = +document.getElementById("new").value;
...

You should put the 你应该把

var x = document.getElementById("new").value;

inside the functions, or it will be executed only once immediately after the script loads (and not work). 在函数内部,否则将仅在脚本加载后立即执行一次(并且不起作用)。

The delay should be checked inside the functions, otherwise the value will be checked only once - when the page loads. 应该在函数内部检查延迟,否则将仅在页面加载时检查一次该值。 If it's inside the functions it will be checked every time the function is triggered. 如果它在函数内部,则每次触发该函数时都会对其进行检查。

$(".fadeInbox").click(function () {
    var x = document.getElementById("new").value;
    $("#section1").delay(x).fadeIn();
    $("#section2").delay(x).fadeIn();
});

$(".fadeOutbox").click(function () {
    var x = document.getElementById("new").value;
    $("#section1").delay(x).fadeOut();
    $("#section2").delay(x).fadeOut();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM