简体   繁体   English

jQuery .append代码有什么问题?

[英]What is wrong with this jQuery .append code?

I'm trying to use this jQuery example code ( source ): 我正在尝试使用以下jQuery示例代码( ):

<script type="text/javascript">
        //change event on delegateno field to prompt new input
        $('#delegateno').change(function() {
        //dynamically create new input and insert after delegate_tel_1
           $("#delegate-tel-1").append("<input type='text' name='delegate-name-2' id='delegate_name_2' />");
});</script>

to append extra form fields to a booking form based on the user changing a number selector: 根据用户更改数字选择器,将额外的表单字段附加到预订表单中:

<input type="number" id="delegateno" value="1">
<input type="text" class="text" name="delegate_name_1" id="delegate-name-1" placeholder="Delegate Name" />
<input type="text" class="text" name="delegate_email_1" id="delegate-email-1" placeholder="Delegate Email" />
<input type="text" class="text" name="delegate_tel_1" id="delegate-tel-1" placeholder="Delegate Telephone"/>

(obviously this form is properly enclosed in form tags etc) (显然,此表单已正确封装在表单标签中,等等)

I realise that at this point the script won't append the new inputs based on the number in the number chosen, I'm just trying to get a basic form working at present - but it doesn't work. 我意识到,此时脚本不会根据所选数字中的数字添加新的输入,我只是想使当前的基本表格正常工作-但它不起作用。 I change the number, nothing happens. 我更改号码,没有任何反应。 I've tried making the number input a text input and changing that, but no dice. 我试图使数字输入成为文本输入并进行更改,但没有骰子。 What am I doing wrong? 我究竟做错了什么?

EDIT: error in ID tags noted, now changed the id in the script to match that of the input field. 编辑:ID标记中指出的错误,现在更改了脚本中的ID以匹配输入字段的ID。 I've also changed to .after as opposed to .append - still no dice. 我也已更改为.after而不是.append-仍然没有骰子。 I now have: 我现在有:

<script type="text/javascript">
  //change event on delegateno field to prompt new input
  $('#delegateno').change(function() {
  //dynamically create new input and insert after delegate_tel_1
  $("#delegate-tel-1").after("<input type='text' name='delegate-name-2' id='delegate_name_2' />");
        });
</script>

Your jQuery is looking for the id delegate_tel_1 , while your HTML has the id delegate-tel-1 . 您的jQuery正在寻找ID delegate_tel_1 ,而您的HTML则具有ID delegate-tel-1

You can either use $("[name=delegate_tel_1]") , to match the name attribute, or use $("#delegate-tel-1") to correctly match the id. 您可以使用$("[name=delegate_tel_1]")来匹配name属性,也可以使用$("#delegate-tel-1")来正确匹配ID。


However, append will insert the new input inside the first, causing a problem. 但是,append会将新input插入第一个input ,从而导致问题。 But if you change .append to .after , jQuery will insert the new <input> right after the first one. 但是,如果将.append更改为.after ,则jQuery将在第一个之后插入新的<input>

probably this is what you are trying to achieve. 可能这就是您要实现的目标。 here is html code 这是HTML代码

<form>
    <input id="delegateno" type="text" value="0" />
    <div class="input-container"></div>
</form>

and JS code 和JS代码

$('#delegateno').change(function () {

    var count = parseInt($(this).val());
    var container = $('.input-container');
    container.empty();
    for (var i = 0; i < count; i++) {
        var inputEl = $('<input type="text" class="text" name="delegate_name_' + i + '" id="delegate-name-' + i + '" placeholder="Delegate Name" />')
        inputEl.appendTo(container);
    }
})

here is the jsfiddle http://jsfiddle.net/YMVeb/2/ 这是jsfiddle http://jsfiddle.net/YMVeb/2/

Yours input ID is delegate-tel-1 , but you have delegate_tel_1 您的输入ID为delegate-tel-1 ,但您拥有的是delegate_tel_1

It has to be: 它一定要是:

$("#delegate-tel-1").append("<input type='text' name='delegate-name-2' id='delegate_name_2' />");

$("#delegate_tel_1").append() will try to append inside the element with id=delegate_tel_1, not after it. $(“#delegate_tel_1”)。append()将尝试在id = delegate_tel_1的元素内追加,而不是在其后。 I think it is probably not working because you are trying to append one input field into another. 我认为这可能不起作用,因为您尝试将一个输入字段附加到另一个输入字段。

Although other folks have correctly pointed out issues with your id's as well 尽管其他人也正确指出了您的身份证件的问题

尝试这样的事情

   $("#delegate-tel-1").after("<input type='text' name='delegate-name-2' id='delegate_name_2' />");

the error is already delegate_tel_1 selector has input field if you append it overwrite the old one try this 该错误已经是委托_电话_1选择器已经输入的字段,如果您追加它覆盖旧的尝试一下

$('#delegateno').keyup(function(){
       $("#DiffSelector").append("<input type='text' name='delegate-name-2' id='delegate_name_2' />");
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM