使用jquery的动态表单问题
我目前的表单根据下拉选择的 onChange 显示表单的添加内容,但是我希望它现在也对新呈现的输入框执行类似的操作,因此,如果用户输入数字“4”,则再次使用 onChange()假设它提供了 4 个新输入。 (所以我想要一个循环)这是我一直在尝试的:
var inputt = document.getElementById("options");
inputt.onchange = function(){
var optionValue = parseInt(this.value);
$('#container').empty();
for (i=0;i<=optionValue;i++)
{
$('<input type="text" value=""/>').appendTo('#container');
}
};
这里是 jsfiddle 注意 - ive仅致力于第一个下拉选择。
忽略破旧的造型,我现在只想要功能。
请帮忙。
问候,
I currently have my form presenting additions to the form depending onChange of my dropdown selection, however I want it also now do a similar thing on the newly presented input box, so, again using onChange() if the user inputs the number "4" say it presents 4 new inputs. (so i want a loop) here is what I have been trying:
var inputt = document.getElementById("options");
inputt.onchange = function(){
var optionValue = parseInt(this.value);
$('#container').empty();
for (i=0;i<=optionValue;i++)
{
$('<input type="text" value=""/>').appendTo('#container');
}
};
here is the jsfiddle note - ive only been working on the first drop down selection.
ignore the shabby styling, i just want functionality right now.
help please.
regards,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用 jQuery,我首先会使用
而不是直接设置 .onchange 属性 - 通常不鼓励以这种方式设置事件处理程序,原因有很多。
但是,您的主要问题似乎是您试图在创建“选项”输入之前绑定此事件处理程序。如果您使用我上面建议的内容,但使用“live”,即使“options”输入尚不存在,它也会工作。
还要注意,javascript 区分大小写。我已将 optionvalue 更正为 optionValue
注意:我无意在这里“回答”您的“问题”,而是试图提供一些帮助。在我看来,您的“问题”涉及修复您的代码,因此这意味着您确实需要针对许多不同问题的大量答案。这只是这些答案之一。
If you are using jQuery, I would first of all use
rather than setting the .onchange property directly - setting event handlers in this way is generally discouraged, for a number of reasons.
However, your main problem seems to be that you are trying to bind this event handler before your "options" input is even created. If you use what I have suggested above, but use "live" instead, it will work even if the "options" input does not yet exist
Also be aware that javascript is case-sensitive. I've corrected optionvalue to optionValue
Note: I'm not intending to "answer" your "question" here, but trying to help a bit. It seems to me that your "question" involves fixing your code, so that means that you really need a lot of answers to a lot of different questions. This is just one of those answers.