函数中未识别名称选择器

Name selector not being recognised in function

本文关键字:选择器 别名 识别 函数      更新时间:2023-09-26

当用户单击按钮时,我正在创建动态输入。这可以创建输入,但在我的函数中,它不响应我指定的名称。换句话说,不启动函数。firebug或我使用的任何其他控制台中都没有错误。我使用名称选择器的方式正确吗?。

如果有人能指出为什么这个功能没有启动,我将不胜感激。非常感谢

更新:我终于这样解决了:

$('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() {
  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() { 
  $('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>'
    }); 
  });

两种可能性。

  1. 请从input标记中注意这一点:

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

    这将产生像box_add[] 0(注意空格)、box_add[] 1等名称。但您正在寻找name="box_add[]",这显然不匹配。

    我的直觉是,你并不是真的想要字段计数,但如果你真的想要,你可能想使用以选择器开头的属性:name^="box_add[]"

  2. 确保在创建完所有input之后执行后一个代码块。