简体   繁体   English

我怎样才能扩展这个?

[英]How can I scale this?

I've got a form that's a few pages long. 我有一个长达几页的表格。 To traverse the form all I'm doing is showing and hiding container divs. 要遍历表单,我正在做的就是显示和隐藏容器div。 The last page is a confirmation page before submitting. 最后一页是提交前的确认页面。 It takes the contents of the form and lays it out so the user can see what he/she just filled out. 它采用表单的内容并将其放置出来,以便用户可以看到他/她刚刚填写的内容。 If they click on one of these it'll take them back to the page they were on (#nav1~3), focus on that field, and let them type in a new value if they need to. 如果他们点击其中一个,它会将他们带回到他们所在的页面(#nav1~3),专注于该字段,并让他们在需要时输入新值。

Using jQuery, I made variables for EVERY field/radio/check/select/textarea/whatever. 使用jQuery,我为每个字段/ radio / check / select / textarea /等做了变量。 If my method seems silly please pwn me but basically, and this method works ok already, but I'm trying to scale it and I don't have any idea how because I don't really know what I'm doing. 如果我的方法看起来很愚蠢,请告诉我,但基本上,这种方法已经正常,但我正在尝试扩展它,我不知道如何因为我真的不知道我在做什么。 Thoughts? 思考?

var field1 = '<a href="#" 
    onclick="$(\'#nav1\').click();$(\'input#field-1\').focus();" 
    title="Click to edit">' + 
    $('input#field-1').val() + '</a>';
    $('#field1-confirm').html(field1);

  var field2 = '<a href="#" 
    onclick="$(\'#nav1\').click();$(\'input#field-2\').focus();" 
    title="Click to edit">' + 
    $('input#field-2').val() + '</a>';
    $('#field2-confirm').html(field2);

And so on, with field3, 4, 5 ~ 25, etc. 等等,有field3,4,5~25等。

If you could help out explaining in non-programmer terms, I'd love you forever. 如果你可以帮助用非程序员的术语解释,我会永远爱你。

I'd start with an introduction to arrays: this one looks pretty decent, for starters. 我首先介绍数组:对于初学者来说, 这个看起来相当不错。

Wrap your head around arrays and loops to get started, and you'll be well served. 围绕阵列和循环缠绕你的头开始,你会得到很好的服务。

Without getting too complicated, you can make a function that handles the repetitive stuff. 没有太复杂,你可以创建一个处理重复的东西的函数。 I haven't tested this, but you'll get the idea: 我没有测试过这个,但你会明白这个想法:

function valField(fieldName,navName) {
    var output = '<a href="javascript://" onclick="$(\''+navName+'\').click();$(\'input#'+fieldName+'\').focus();" title="Click to edit">' + $('input#'+fieldName).val() + '</a>';
 $('#'+fieldName+'-confirm').html(output);
}

valField("field-1","nav1")
valField("field-2","nav1")
valField("field-293","nav3")

When you get better at Javascript, you would probably just make a loop to handle all these "valField()" calls, or you would write something that would inspect your form, find what's there and generate event handlers to glue it all together automatically. 当你在Javascript上变得更好的时候,你可能只是做一个循环来处理所有这些“valField()”调用,或者你会写一些东西来检查你的表单,找到那里的东西并生成事件处理程序将它们全部粘合在一起。 That's certainly not "n00bware", but it gives you something to think about. 这肯定不是“n00bware”,但它给你一些思考的东西。

Also instead of using this on your output: 而不是在输出上使用它:

$(\''+navName+'\').click();

You could replace it with whatever code is actually in the onclick of the nav tab. 您可以使用导航选项卡的onclick中的实际代码替换它。

There are dozens of ways to solve this problem. 有很多方法可以解决这个问题。 Take it one step at a time. 一步一步。

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

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