尝试了几件事并没有成功。
想法是从表单中抓取值并推送到数组并将其全部添加...
pageNext()函数应该只是遍历表单,隐藏前一个并显示下一个,并且在最后一页上的想法是让submitBtn显示一些内容。
但由于某种原因,我将NaN推送到数组,所以我试图找到一种方法从我的数组中过滤那些NaN?还是不把它们放在首位?
欣赏。
$(document).ready(init);
var answersTotal = [];
var answer;
function init(){
$('#nextBtn').on('click', pageNext);
}
function grabAnswer(){
answers = $("select option:selected");
values = $.map(answers ,function(option) {
return parseInt(option.value);
});
answersTotal.push(values);
// answersTotal = answersTotal.filter(function(n){ return n != NaN });
// console.log('answersTotal after filter: ', answersTotal);
}
function pageNext(){
$('#nextBtn').on('click', function(){
grabAnswer();
console.log('values: ', values);
console.log(answersTotal);
$('.current').removeClass('current').hide().next().show().addClass('current');
if ($('.current').hasClass('last')){
$('#nextBtn').hide();
$('#submitBtn').show();
};
});
};
.select2, .select3, .select4, .select5 {
display: none;
}
#submitBtn {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="input-field first current select1">
<p>Q1?</p>
<select>
<option value="" disabled selected>Choose your option</option>
<option value="1">a</option>
<option value="2">b</option>
<option value="3">c</option>
</select>
</div>
<div class="input-field select2">
<p>q2</p>
<select>
<option value="" disabled selected>Choose your option</option>
<option value="2">a</option>
<option value="5">b</option>
<option value="7">c</option>
<option value="10">d</option>
<option value="12">e</option>
</select>
<!-- <label>Materialize Select</label> -->
</div>
<div class="input-field select3">
<p>What do you Find More Interesting?</p>
<select>
<option value="" disabled selected>a</option>
<option value="1">b</option>
<option value="3">c</option>
</select>
</div>
答案 0 :(得分:4)
从任何数组中删除NaN的简单方法是使用Array.prototype.filter
+ Number.isNaN()
:
const newArray = oldArray.filter(function (value) {
return !Number.isNaN(value);
});