函数中未识别名称选择器
Name selector not being recognised in function
当用户单击按钮时,我正在创建动态输入。这可以创建输入,但在我的函数中,它不响应我指定的名称。换句话说,不启动函数。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>'
});
});
两种可能性。
-
请从
input
标记中注意这一点:$(InputsWrapper).append('...name="box_add[] '+FieldCount+'"...'); // ^^^^^^^^^^^^^^^^^^^^^^^^^^
这将产生像
box_add[] 0
(注意空格)、box_add[] 1
等名称。但您正在寻找name="box_add[]"
,这显然不匹配。我的直觉是,你并不是真的想要字段计数,但如果你真的想要,你可能想使用以选择器开头的属性:
name^="box_add[]"
。 -
确保在创建完所有
input
之后执行后一个代码块。
相关文章:
- Windows8应用程序(html&Javascript):从图片库(除了文件选择器)显示图像的另一种方式
- 在动态创建的元素上获取对特定选择器的引用
- AngularJS-在JSON选择器中使用变量名
- 每当您在选择器内移动鼠标时,悬停功能就会重复
- 使用CSS或js,使用动态选择器选择任意li的下一个元素
- jQuery最近父级的数据属性选择器
- 为什么不'在JQuery中找到第二个css选择器的工作
- 为什么 .focus() 不起作用,而 .css(“color”,“red”) 在同一个选择器上起作用
- jquery IE7中的第n个选择器
- Webdriver.io pageObject模式-通过传递参数来定义元素选择器
- jquery日期选择器年份范围默认值
- jquery日期选择器失去了交互性
- 正在搜索JavaScript日期选择器滑块
- 如何将返回的值应用于多个不同位置的多个选择器
- 每个选择器的Jquery css颜色都在变化,但字体大小却没有变化
- 如何在多个jQueryUI日期选择器中屏蔽特定日期
- jquery日期选择器显示与值不同的文本
- jQuery选择器无法正常工作
- 文档就绪提供了错误的选择器高度
- 未捕获的语法错误:无法在“文档”上执行“查询选择器”