我.inArray()
用来检查我的数组是否有“0”值,然后return false;
如果有零则破坏函数。
这是我目前正在使用的代码:
$(document).ready(function() {
$('#calc').click(function(e) {
var drop = $(".drop"),
c = [],
g = [];
//Push values into an array.
drop.each(function() {
var $t = $(this),
cals = $t.val(),
option = $(':selected', this),
gly = parseFloat(option.attr('rel'));
g.push(gly * 2);
c.push(cals * 2);
});
var inthere = jQuery.inArray('0', c) > -1;
//don't calculate if array is empty
if (c.length === 0) {
return false;
}
//don't calculate with a '0' value in array
if (inthere == true) {
return false;
}
//shouldn't display if you haven't added a dropdown OR if the dropdown stayed on the "Default" option
alert('Passed');
});
$('#add').click(function() {
$('#contain').append('<select class="drop"><option value="" rel="" data-default="true">Default</option><option value="1" rel="2">Option 1</option><option value="3" rel="4">Option 2</option></select>');
});
});
当用户离开option
“默认”选项时,传递给的值c
应该是 0。出于某种原因,代码将通过,除非我没有在下拉列表中添加...
如果数组中有“0”,如何防止代码继续执行c
???
这是小提琴