gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:16:26 24 4
gpt4 key购买 nike

我在客户端 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>

问题

如何迭代表单的所有字段,以便将字段内容放入 Amber 字典中,并以字段名称作为键、文本内容作为值?

Stephen-Eggermont 和 MKroenert 回答后问题的新版本

如何获取表单所有字段的值,以便将它们放入以字段名称为键、文本内容为值的 Amber 字典中?

或者是否有一种惯用的方法来创建表单并检索值?

注意:如果可以使内容更具可读性,可以使用 Amber 代码构建表单。

引用文献

回答后编辑:FileIn 代码

MKroenert 提供的答案效果很好

下面是我测试过的他的代码。它可以直接归档到工作区中

    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.
]
! !

最佳答案

我重复使用了 this SO question 中的代码并用 Amber 重写它来解决你问题的第一部分。以下是迭代所有输入字段的方法:

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

This Amber recipe需要访问 JavaScript this

将输入字段的名称和值打印到 JavaScript 控制台可以如下完成:

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

将值放入字典中:

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

至于你问题的第二部分,Amber 中有一个 Web 包,其中包含用于生成 HTML 页面的类。您要做的就是创建 Widget 的子类并实现 renderOn: html 方法。作为 html 参数传入的对象是 HTMLCanvas 类型,可用于创建如下 HTML 表单:

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

这是一个完整的示例。以此为起点,并意识到这可能不是最有效的做事方式

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).
].

在运行的 Amber 实例中实现此类后,可以使用以下代码来执行它:

AmberFormExample new appendToJQuery: 'body' asJQuery

关于smalltalk - 如何获取表单所有字段的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27664859/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com