简体   繁体   English

无法在功能中识别名称选择器

[英]Name selector not being recognised in function

I am creating dynamic inputs when a user clicks a button. 我在用户单击按钮时创建动态输入。 This is creating the inputs ok, but in my function, it is not responding to the name I have assigned. 这样就可以创建输入了,但是在我的函数中,它没有响应我分配的名称。 In other words, not firing the function. 换句话说,不触发该功能。 There are no errors in firebug or any other console I use. 我没有使用firebug或任何其他控制台中的任何错误。 Am I using the name selector in the correct way?. 我是否以正确的方式使用名称选择器?

I would be grateful if someone could point out why this function is not firing. 如果有人可以指出为什么此功能未触发,我将不胜感激。 Many thanks 非常感谢

UPDATE : I finally managed to solve it like this: 更新 :我终于设法解决这个问题:

$('input[name="box_add[]'+FieldCount+'"]').inputlimiter({
    limit: 1,
    limitBy: 'words',
    remText: 'You only have %n word%s remaining...',
    limitText: '<b><font color=\"red\">Field limited to %n box%s.</font></b>'
    });

Function to create the inputs. 创建输入的功能。 This is working ok 这样就可以了

$(function() {

  var MaxInputs       = 19; //maximum input boxes allowed
  var InputsWrapper   = $("#INTKInputsWrapper"); //Input boxes wrapper ID
  var AddButton       = $("#INTKAddMoreFileBox"); //Add button ID

  var x = InputsWrapper.length; //initlal text box count
  var FieldCount=1; //to keep track of text box added

  $(AddButton).click(function (e)  //on add input button click
  {
  if(x <= MaxInputs) //max input box allowed
  {
  FieldCount++; //text box added increment
  //add input box
  $(InputsWrapper).append('<div><input style="margin-left: 16px; margin-bottom: 12px; width: 250px; height:30px;" type="text" class="boxadddef" name="box_add[] '+FieldCount+'" required="required" /><a href="#" class="removeclass"><img src="/domain/users/css/images/redclose.png" style="margin-left: 10px; margin-right:10px;" /></a><span class="removespan" style="margin-left:2px;font-size:10px;color: grey;">Remove</span></div>');
  x++; //text box increment
  }
  return false;
  });

  $("body").on("click",".removeclass", function(e){ //user click on remove text
  if( x > 1 ) {
  $(this).parent('div').remove(); //remove text box
  x--; //decrement textbox
  FieldCount--;
  }
  return false;
  }) 

  });

Function that is incorrect 功能不正确

$(function() { 
  $('input[name="box_add[]"]').inputlimiter({
        limit: 1,
        limitBy: 'words',
        remText: 'You only have %n word%s remaining...',
        limitText: '<b><font color=\"red\">Field limited to %n box%s.</font></b>'
    }); 
  });

Two possibilities. 两种可能性。

  1. Note this from your input markup: input标记中注意这一点:

     $(InputsWrapper).append('...name="box_add[] '+FieldCount+'"...'); // ^^^^^^^^^^^^^^^^^^^^^^^^^^ 

    That will result in a names like box_add[] 0 (note the space), box_add[] 1 , etc.. But you're looking for name="box_add[]" , which obviously wouldn't match. 这将导致一个名称,例如box_add[] 0 (请注意空格), box_add[] 1等。但是,您正在寻找的name="box_add[]"显然不匹配。

    My gut says you didn't really want the field count there, but if you really did, you might want to use the attribute-starts-with selector instead: name^="box_add[]" . 我的直觉是您实际上并不希望字段计数在那里,但如果确实需要,那么您可能想要使用attribute-starts-with选择器: name^="box_add[]"

  2. Ensure you're doing the latter code block once all the input s have been created. 一旦所有input均已创建,请确保您正在执行后面的代码块。

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

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