轻微的jquery blip导致字段重复

时间:2009-09-26 09:37:29

标签: jquery html

我正在尝试让jquery将选择框的值添加到隐藏的输入字段和要在公共视图中显示的div。

这是我的代码

<script type='text/javascript'>
$("#departments_submit").click(function(){

    added_departments = new Array();
    var depo = $("#depo_list").val();
    alert(jQuery.inArray(depo, added_departments))
        if(jQuery.inArray(depo, added_departments) != -1)
        {
            return false;
        }
        else
        {
            added_departments.push(depo);

        }
        $("#depo_added_list").append("<li>" + depo + "</li>");
        var current_value = $("#departments").val();
        if(current_value)
        {
            $("#departments").val(current_value + "," + depo);
        }
        else
        {
            $("#departments").val(depo);
        }
        return false;
    alert(added_departments)
});

</script>

我遇到的问题是,当用户提交选择表单时,如果已经添加了该项目,它仍会将其添加到字段和div中,当它不应该时。

任何想法?

1 个答案:

答案 0 :(得分:1)

尝试:

<script type='text/javascript'>
added_departments = new Array();

$("#departments_submit").click(function() {
        var depo = $("#depo_list").val();
        alert(jQuery.inArray(depo, added_departments))

        if (!jQuery.inArray(depo, added_departments)) {
               added_departments.push(depo);
               $("#depo_added_list").append("<li>" + depo + "</li>");
               var current_value = $("#departments").val();

               if (current_value) $("#departments").val(current_value + "," + depo);
               else $("#departments").val(depo);

               alert(added_departments)
        }
});
</script>

如果这不起作用,请发布HTML代码,我会再看看。