【问题标题】:JQuery check for empty listbox itemJQuery 检查空列表框项
【发布时间】:2014-10-01 23:29:34
【问题描述】:

我有将下拉菜单中的项目添加到列表框中的代码。当用户提交表单时,它会通过列表框并选择所有项目,然后更新表格。

如果用户从列表框中删除了所有项目,我在代码中添加了一个空白项目到列表框,这样列表框仍然可以更新。我必须这样做,因为如果列表框中没有项目,那么它不会被更新并且旧项目会保留。

    $.each(aListBoxes, function (idx, listBox) {
    //If nothing in listbox add blank item so it can get updated
    if ($("#" + listBox + " option").length < 1) {
    ($("#" + listBox).append('<option value=""></option>'));
    }

现在,我想检查列表框中是否有超过 1 项,如果存在则删除此空白项。

    if ($("#" + listBox + " option").length > 1) {
    $("#" + listBox + " option").each(function () {

    //if item value is empty then remove

到目前为止的整个脚本:

<script type="text/javascript">
    //for pre-selection of all elements in every list box
    $(document).ready(function () {
        var aListBoxes = [];
        // get a list of all the listboxes on the form
        $("[id*=" + "lbx" + "]:visible").each(function () {
            aListBoxes.push(this.id);
        });

        //on btnSubmit click because the form gets posted each time an autopostback field is hit.
        $("input[name=btnSubmit]").click(function () {
            //just before submission, go through each of the listboxes and if they contain anything, select everything.
            $.each(aListBoxes, function (idx, listBox) {
                //If nothing in listbox add blank item so it can get updated
                if ($("#" + listBox + " option").length < 1) {
                    ($("#" + listBox).append('<option value=""></option>'));
                }
                //If more than 1 item check for blank and remove
                if ($("#" + listBox + " option").length > 1) {
                    $("#" + listBox + " option").each(function () {
                        //if empty
                        //remove
                        });
                }

                //select all before submitting
                if ($("#" + listBox + " option").length > 0) {
                    $("#" + listBox + " option").each(function () {
                        $(this).prop('selected', 'selected')
                    });
                }
            });
        });
    });
</script>

【问题讨论】:

    标签: jquery listbox


    【解决方案1】:

    我希望空白选项意味着它们的值为空。试试这个:-

        if ($("#" + listBox + " option").length > 1) {
              $(this).each(function () {  
                   if($(this).val()=="")
                        $(this).remove();
              });
        }
    

    如果你的意思是空白选项的文本是空的,那么写$(this).text()==""而不是$(this).val()==""

    【讨论】:

    • 欢迎您。如果他们正在寻找相同的答案,请接受,以便其他人也知道答案。快乐编码!
    【解决方案2】:

    jQuery filter 是专门为这种类型设计的,避免使用each

    if ($("#" + listBox + " option").length > 1) {
          $(this).filter( function () {
              return $(this).val()=="";
          }).remove();
    }
    

    【讨论】:

      猜你喜欢
      • 2011-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多