简体   繁体   English

如何获取表单所有字段的值?

[英]How do I get the values of all the fields of a form?

I have a HTML form like this in a client side Amber solution 我在客户端Amber解决方案中有这样的HTML表单

<form id="myForm1">
  Creator:  <input type="text" name="creator" />
  <br>
  Title:  <input type="text" name="title" />
  <br>
  Description:  <input type="text" name="description" />
  <br>
  Doctype:  <input type="text" name="doctype" />
  <br>
  Tags:  <input type="text" name="tags" />
</form>

Question

How do I iterate through all of the fields of the form in order to put the content of the fields into a Amber dictionary with the field name as key and the text content as value? 我如何遍历表单的所有字段,以将字段内容放入Amber字典中,并将字段名称作为键,将文本内容作为值?

New version of the question after answer by Stephen-Eggermont and MKroenert Stephen-Eggermont和MKroenert回答后的新版本问题

How do I get the values of all the fields of the form in order to put them into an Amber dictionary with the field name as key and the text content as value? 我如何获取表单中所有字段的值 ,以便将它们放入Amber字典中,并将字段名称作为键并将文本内容作为值?

Or is there an idiomatic way to create a form and retrieve the values? 还是有一种惯用的方式来创建表单并检索值?

Note : The form may be constructed with Amber code if this makes things more readable. 注意 :如果可以使表单更具可读性,则可以使用Amber代码构造表单。

References 参考

Edit after answer: FileIn code 答案后编辑:FileIn代码

The answer provided by MKroenert works fine MKroenert提供的答案很好用

Below is his code I tested. 以下是我测试过的代码。 It may be filed in directly in a workspace 它可以直接归档在工作空间中

    Widget subclass: #AmberFormExample
    instanceVariableNames: 'dictionary inputs'
    package: 'TodoList'!

!AmberFormExample methodsFor: 'not yet classified'!

collectValues
    inputs do: [ :each |
        dictionary at: (each asJQuery attr: 'name')
            put: (each asJQuery val).
        ].

Transcript show: dictionary printString
!

initialize
    dictionary := Dictionary new.
    inputs := Array new.
!

renderInput: inputName on: html
    html p: [
        html label with: inputName.
            inputs add: (html input id: inputName;
                name: inputName;
                yourself)]
!

renderOn: html
    inputs removeAll.
    html form id: 'myForm1'; with: [
        #('Creator' 'Title' 'Description' 'Doctype' 'Tags') do: [ :each |
            self renderInput: each on: html]].
    html button
        with: 'Collect Inputfield Values';
        onClick: [
            self collectValues.
            ]
! !

I reused the code from this SO question and rewrote it in Amber to address the first part of your question. 我重用了这个问题的代码,并在Amber中重写了代码,以解决问题的第一部分。 Here is how you iterate over all input fields: 这是您遍历所有输入字段的方式:

(('#myForm1 *' asJQuery)
    filter: ':input')
        each: [ :thisArg :index |
            console log: thisArg ] currySelf

This Amber recipe is required to get access to the JavaScript this . 要访问JavaScript this需要此琥珀色配方

Printing both name and value of the input fields to the JavaScript console can be done like this: 可以将输入字段的名称和值同时打印到JavaScript控制台,如下所示:

(('#myForm1 *' asJQuery)
    filter: ':input')
        each: [ :thisArg :index |
            console log: (thisArg asJQuery attr: 'name').
            console log: (thisArg asJQuery val)] currySelf

Putting the values into a dictionary: 将值放入字典中:

| dict |
dict := Dictionary new.
(('#myForm1 *' asJQuery)
    filter: ':input')
        each: [ :thisArg :index |
            dict at: (thisArg asJQuery attr: 'name')
                put: (thisArg asJQuery val)] currySelf

As for the second part of your question, there is the Web package in Amber which contains Classes for generating HTML pages. 至于问题的第二部分,Amber中有一个Web包,其中包含用于生成HTML页面的类。 What you do is to create a subclass of Widget and implement the renderOn: html method. 您要做的是创建Widget的子类并实现renderOn: html方法。 The object passed in as html parameter is of type HTMLCanvas and can be used to create an HTML form like this: 作为html参数传入的对象的类型为HTMLCanvas ,可用于创建如下的HTML表单:

renderOn: html
    html form with: [
        html input id: 'creator'.
        html input id: 'title'.]

Here is a complete example. 这是一个完整的例子。 Take it as a starting point and be aware that it may not be the most efficient way of doing things 以它为起点,并意识到这可能不是最有效的处理方式

Widget subclass: #AmberFormExample
    instanceVariableNames: 'dictionary inputs'
    package: 'Examples'

AmberFormExample>>initialize
    dictionary := Dictionary new.
    inputs := Array new.

AmberFormExample>>renderOn: html
    inputs removeAll.
    html form id: 'myForm1'; with: [
        #('Creator' 'Title' 'Description' 'Doctype' 'Tags') do: [ :each |
            self renderInput: each on: html]].
    html button
        with: 'Collect Inputfield Values';
        onClick: [
            self collectValues.
            ]

AmberFormExample>>renderInput: inputName on: html
    html p: [
        html label with: inputName.
            inputs add: (html input id: inputName;
                name: inputName;
                yourself)]

AmberFormExample>>collectValues
    inputs do: [ :each |
        dictionary at: (each asJQuery attr: 'name')
            put: (each asJQuery val).
        ].

After implementing this class in a running Amber instance the following code can be used to execute it: 在正在运行的Amber实例中实现此类之后,可以使用以下代码来执行它:

AmberFormExample new appendToJQuery: 'body' asJQuery

There is a lot of duplication in your form. 您的表单中有很多重复项。 You might want to take a look at HTMLCanvas and how it is used in IDE. 您可能想看一下HTMLCanvas及其在IDE中的使用方式。

You could add a method renderField: aFieldName on: aCanvas and reuse that 5 times. 您可以在renderField: aFieldName on: aCanvas添加方法renderField: aFieldName on: aCanvas并重复使用5次。 Did you take a look at Seaside? 您看过海边吗?

The end result should be something like 最终结果应该是这样的

renderOn: html
    html form with: [
        #('Creator' 'Title' 'Description' 'Doctype' 'Tags') do: [:each |
            self renderField: each on: html]

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

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