1

我想检查文本字段 newTeamName 是否已经在存储在选择框中的团队名称列表中。不幸的是,我的代码不起作用 - 它有什么问题?哦,我没有控制台问题。

    optionList = [];
    $('#chooseTeam option').each(function() {
        optionList.push($(this).val())
    });
    if (form.newTeamName.value in optionList) {
        $("#text-error").html("Team exists");
        $('#text-error').fadeIn(400).delay(3200).fadeOut(800);
        return false;
    }

小更新:

哦,我的 form.name.value 工作正常,因为它们适用于其他 if 语句。

4

3 回答 3

1

optionList是一个in用于对象属性(或数字数组索引)的数组,您可以使用它indexOf来测试一个值是否在数组中

optionList = [];
$('#chooseTeam option').each(function() {
    optionList.push($(this).val())
});
if (optionList.indexOf(form.newTeamName.value) > -1) {
    $("#text-error").html("Team exists");
    $('#text-error').fadeIn(400).delay(3200).fadeOut(800);
    return false;
}
于 2012-12-25T07:04:46.030 回答
0

修复。

    $('#chooseTeam option').each(function() {
        if (form.newTeamName.value == $(this).val()){
            $("#text-error").html("Team exists");
            $('#text-error').fadeIn(400).delay(3200).fadeOut(800);
            return false;
        }
    });
于 2012-12-25T07:08:17.657 回答
0

尝试这样的事情

  my_array = Array();

  //To find if a value or element exists in an array
  if (my_array.indexOf(‘find_this_value’) != -1)
  {
        alert(‘Value exists in array.’);
  }

 //To find if a value or element DOES NOT exist in an array
 if (my_array.indexOf(‘find_this_value’) == -1)
 {
     alert(‘Value does not exist in array.’);
 }
于 2012-12-25T07:41:52.673 回答