我试图使用splice从数组中删除一个值。从0开始到0结束的splice,但它没有删除索引为0的值。我添加了一个名为getItemRow的函数来检查该物种的索引,它返回0。我将数组的值转储到警报中,并且仍然输出应该已被删除的species。无效元素.splice(indexValue,indexValue);适用于不是0的索引。为什么会这样,如何删除具有索引为0的值?
JavaScript代码:
var invalidElement = new Array("species", "alias", "gender", "breeding", "birth_date");
//This function will be removed once fixed!!
function getItemRow()
{
var myPosition=-1
for (i=0;i<invalidElement.length;i++)
{
if(invalidElement[i]=="species") {
myPosition = i;
break;
}
}
alert(myPosition)
}
function validateElement(formId, element, selector, errorContainer)
{
getItemRow()//for testing purposes
//var indexValue = $.inArray(element, invalidElement);
var indexValue = invalidElement.indexOf(element);
alert(element);
$.ajax({
type: 'POST',
cache: false,
url: "validate_livestock/validate_form/field/" + element,
data: element+"="+$(selector).val(),
context: document.body,
dataType: 'html',
success: function(data){
if (data == "false")
{
$(errorContainer).removeClass('element_valid').addClass('element_error');
invalidElement = element;
alert(invalidElement.join('\n'))//for testing purposes
//alert(indexValue);
}
else
{
$(errorContainer).removeClass('element_error').addClass('element_valid');
invalidElement.splice(indexValue, indexValue);
alert(invalidElement.length);//for testing purposes
alert(invalidElement.join('\n'))//for testing purposes
}
}
});
}
$("#species").change(function(){
validateElement('#add_livestock', 'species', '#species', '.species_error_1')
});