繁体   English   中英

用户输入为javascript变量

[英]user input as javascript variable

我在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();

});

input中的var xstring而不是int ,因此

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

要么

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

你应该把

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

在函数内部,否则将仅在脚本加载后立即执行一次(并且不起作用)。

应该在函数内部检查延迟,否则将仅在页面加载时检查一次该值。 如果它在函数内部,则每次触发该函数时都会对其进行检查。

$(".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