单击时重复输入,在单击元素之前插入 - jQuery

时间:2016-10-27 10:47:42

标签: javascript jquery html clone

我有这样的标记:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="input_one[]">
    <label class="duplicate" for="input_one[]">Add another car</label>
qryString = "Select distinct(description), merchant_name from(" & "|" & "select distinct description, merchant_name from table" & Range("G3").value
        qryString = qryString & "|" & patternString & "|" & "Union all" & "|"
        qryString = qryString & "select distinct description, merchant_name from table" & Range("G4").value
        qryString = qryString & "|" & patternString & "|" & ") order by merchant_name, description"
        qryString = Trim(qryString)
        Range("H14").value = qryString
        Range("H14").WrapText = False
        Range("H14").Select
        Selection.TextToColumns Destination:=Range("I14"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
        :="|", FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, _
        1), Array(6, 1), Array(7, 1), Array(8, 1), Array(9, 1), Array(10, 1), Array(11, 1), Array(12 _
        , 1), Array(13, 1)), TrailingMinusNumbers:=True
        Range("I14").Select
        Range(Selection, Selection.End(xlToRight)).Select
        Selection.Copy
        Range("H16").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=True

这不重复,或提供错误,所以我不知道为什么这不起作用?

2 个答案:

答案 0 :(得分:1)

它正常工作,但您需要在运行代码之前等待dom-structur加载。将所有内容放在documet.ready中就像这样:

$(document).ready(function() {
  $('label.duplicate').click(function(e) {
    e.preventDefault();
    var _for = $(this).attr('for');

    $('input[name="' + _for + '"]').clone().insertBefore($(this));
  });
})

或者在您页面上最后插入您的代码。在此处详细了解:https://learn.jquery.com/using-jquery-core/document-ready/

答案 1 :(得分:0)

<input type="text" name="input_one[]">
<label class="duplicate" for="input_one[]">Add another car</label>

只需.duplicate即可复制:

$(function(){ 
$('.duplicate').click(function(e) {
    e.preventDefault();
    var _for = $(this).attr('for');

    $('input[name="' + _for + '"]').clone().insertBefore($(this));
  });
}