代码之家  ›  专栏  ›  技术社区  ›  superUntitled

jQuery错误,仅适用于资源管理器

  •  0
  • superUntitled  · 技术社区  · 17 年前

    该代码的目的是允许用户在表单中添加多个表单字段(如果用户想要多个电话号码或多个网址)。

    ///////////////////////////////
    Line: 240
    Char: 5
    Error: 'this.id.3' is null or not an object
    Code: 0
    ///////////////////////////////
    

    <ul>
    <li>
        <ul>
        <li class="website">
            <input id="website1_input" name="website[1][input]" type="text" value="www.test.org"/>
            <input type="checkbox" id="website1_delete" name="website[1][delete]" class="element radio" value="1" />
            <label for="website1_delete">Delete</label>     
        </li>
        </ul>
        <a href="#" id="addWebsite">add another website</a>
        </li>
    </ul>
    

    所有的lis都应该有一个特定的类,比如“电话”或“地址” “addPhone”或“addAddress”。href属性应为“#”。

    function makeAddable(theClass) {
      $('#add' + theClass[0].toUpperCase() + theClass.substr(1)).click(function() {
        var numItems = $('.' + theClass).length;
        var newItem = $('.' + theClass).eq(numItems - 1).clone();
        newItem.find('input[type=text]').val('');
        numItems++; // number in the new IDs
    
        // keep ids unique, linked to label[for], and update names
        // id & for look like: phone1_type, phone1_ext, phone13_p1, etc.
        // names look like: phone[1][type], phone[1][ext], phone[13][p1], etc.
        newItem.find('[id^=' + theClass + ']').each(function(i) {
          var underscoreIndex = this.id.indexOf('_');
          var idNum = this.id.substring(theClass.length, underscoreIndex);
          this.id = this.id.replace(idNum, numItems);
        });
        newItem.find('[for^=' + theClass + ']').each(function(i) {
            var jqthis = $(this);
            var forAttr = jqthis.attr('for');
            var underscoreIndex = forAttr.indexOf('_');
            var idNum = forAttr.substring(theClass.length, underscoreIndex);
            forAttr = forAttr.replace(idNum, numItems);
            jqthis.attr('for', forAttr);
        });
        newItem.find('[name^=' + theClass + ']').each(function(i) {
            var jqthis = $(this);
            var nameAttr = jqthis.attr('name');
            var bracketIndex = nameAttr.indexOf(']');
            var idNum = nameAttr.substring(theClass.length + 1, bracketIndex);
            nameAttr = nameAttr.replace(idNum, numItems);
            jqthis.attr('name', nameAttr);
        });
    
        $(this).prev('ul').append(newItem);
        return false;
      });
    }
    
    // Automatically enable all <a href="#" id="addSomething"> links
    $(function() {
      $('a[href=#][id^=add]').each(function(i) {
        makeAddable( this.id[3].toLowerCase() + this.id.substr(4) );
      });
    });
    
    3 回复  |  直到 17 年前
        1
  •  4
  •   Fabien Ménager    17 年前
    this.id.charAt(3).toLowerCase()
    
        2
  •  1
  •   Josh Stodola    17 年前

    this.id[3]
    

    this.id.substr(3, 1);
    
        3
  •  1
  •   jcoffey    17 年前

    function addInput(inputType) {
        var items, itemNumber, lastItem, newListItem, parentList, inputName, inputId, deleteId, thisId;
    
        items = $('li.' + inputType);
        itemNumber = items.length + 1;
        lastItem = $(items[itemNumber-2]);
        newListItem = lastItem.clone();
        parentList = lastItem.parents('ul:first');
    
        inputId = inputType + itemNumber + '_input';
        deleteId = inputType + itemNumber + '_delete';
    
        $(':input', newListItem).each(function() {
            inputName = $(this).attr('name').replace(/\[\d*\]/, '['+itemNumber+']');
            thisId = $(this).attr('id').match(/_delete/) ? deleteId : inputId
    
            $(this)
                .val('')
                .removeAttr('checked')
                .attr('id', thisId)
                .attr('name', inputName)
            }).end()
            .find('label').attr('for', deleteId).end()
            .appendTo(parentList)
        ;
    }
    
    $(function() {
        $('a[href=#][id^=add]').live('click', function() {
            addInput(this.id.substr(3).toLowerCase());
        });
    });