javascript/jquery 调查进度条
我试图在每次用户回答问题时让进度条前进(即使页面上有多个问题)。所以我在页面的开头/顶部设置了变量:
<div id="progress"><p id="counter"><span id="percent">0%</span></p></div>
<script>var count = 0;
var total = 79;
var progress = 0;</script>
然后我对大多数带有无线电输入的问题做了一些 jQuery,它们工作得很好(并且如果您更改问题上的选项,则不会增加计数)。但是,对于复选框和选择,它不起作用:
$('input[name="race"]').one("click", function(){
var n = 0;
if (n==0) {
n = ++n;
count = ++count;
progress = count/total;
$("#counter").css("width", progress);
$("#percent").text(progress + "%")
}
});
如果我删除 if 语句,该函数可以工作,但它会增加每个复选框的 count
。
我还想将 progress
修剪为仅 3 个字符,但它不喜欢 progress =progress.slice(0,3);
或 `progress=progress.substr (0,10)
I'm trying to have a progress bar advance every time a user answers a question (even with multiple questions on a page). So I set up to variables at the beginning/top of the page:
<div id="progress"><p id="counter"><span id="percent">0%</span></p></div>
<script>var count = 0;
var total = 79;
var progress = 0;</script>
Then I do a bit of jQuery for most of the questions with radio inputs, and they work fine (and don't increase count if you change your option on the question). However, with checkboxes and selects, it's not working as well:
$('input[name="race"]').one("click", function(){
var n = 0;
if (n==0) {
n = ++n;
count = ++count;
progress = count/total;
$("#counter").css("width", progress);
$("#percent").text(progress + "%")
}
});
If I remove the if-statement, the function works, but it increases count
for each checkbox.
I would also like to trim progress
to just 3 characters, but it doesn't like progress = progress.slice(0,3);
or `progress = progress.substr(0,10)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于 if 语句,它需要另一个 = 符号。这样逻辑应该是正确的。哦,您在上面的行中将 n 设置为字符串,但将其与下面的行中的 int 进行比较。您需要选择其中之一。从 n=n++ 行开始,我相信您会想要更改 var n = 0;而不是 var n =“0”;
然后对于进度变量,您尝试获取 int 的子串。因此,您需要在修剪之前将 int 转换为字符串。但作为一个 int ,它不应该需要被修剪。
For the if statement it needs another = sign. That should get the logic right. Oh and you set n as a string in the line above, but compare it to an int in the line below. You will need to choose one or the other. From the line n=n++ I believe you will want to change var n = 0; instead of var n ="0";
Then for the progress variable you are trying to take the substr of an int. Thus you would need to convert the int to a string before trimming. But as an int it shouldn't need to be trimmed anywise.
假设您只有单选按钮、复选框和选择。这应该可以解决问题
Assuming that you have only radio buttons, checkboxes and selects. This should do the trick