使用jquery的动态表单问题

发布于 2024-10-27 07:24:19 字数 568 浏览 3 评论 0原文

我目前的表单根据下拉选择的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叶落知秋 2024-11-03 07:24:19

如果您使用 jQuery,我首先会使用

$("#options").bind("change",function(){
  //your code here
});

而不是直接设置 .onchange 属性 - 通常不鼓励以这种方式设置事件处理程序,原因有很多。

但是,您的主要问题似乎是您试图在创建“选项”输入之前绑定此事件处理程序。如果您使用我上面建议的内容,但使用“live”,即使“options”输入尚不存在,它也会工作。

//i've attached keyup in this example, but you could try modifying it to "change"
$("#options").live("keyup",function(e){
  var optionValue = parseInt(e.target.value);
  $('#container').empty();
  //note optionValue, not optionValue
  //note "<" rather than "<=", otherwise if you type "1" you'll get 2 inputs
  for (i=0;i<optionValue;i++) 
  {
    $('<input type="text" value=""/>').appendTo('#container');        
  }    
});

还要注意,javascript 区分大小写。我已将 optionvalue 更正为 optionValue

注意:我无意在这里“回答”您的“问题”,而是试图提供一些帮助。在我看来,您的“问题”涉及修复您的代码,因此这意味着您确实需要针对许多不同问题的大量答案。这只是这些答案之一。

If you are using jQuery, I would first of all use

$("#options").bind("change",function(){
  //your code here
});

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

//i've attached keyup in this example, but you could try modifying it to "change"
$("#options").live("keyup",function(e){
  var optionValue = parseInt(e.target.value);
  $('#container').empty();
  //note optionValue, not optionValue
  //note "<" rather than "<=", otherwise if you type "1" you'll get 2 inputs
  for (i=0;i<optionValue;i++) 
  {
    $('<input type="text" value=""/>').appendTo('#container');        
  }    
});

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文