6

我刚刚开始使用lift,我现在正试图将普通表单更改为ajax 表单,但processEntryAdd从未调用过该方法。

def addUser(xhtml : Group) : NodeSeq = {

    var firstName = ""
    var lastName = ""

    def processEntryAdd() {
        Log.info("processEntryAdd: " + firstName + ", " + lastName)
    }

    SHtml.ajaxForm(
        bind("entry", xhtml,
             "firstName" -> SHtml.text(firstName, 
                 (x) => {
                     Log.info("Setting firstName to " + x); 
                     firstName = x
                 }),
             "lastName" -> SHtml.text(lastName, 
                 (x) => {
                     Log.info("Setting lastName to " + x); 
                     lastName = x
                 }),
             "submit" -> SHtml.submit("Add user", processEntryAdd),
        ))
}

知道如何实现我想要做的事情,或者为什么上面的代码不起作用。按下按钮时会提交两个表单字段的值,并且设置了两个局部变量firstNamelastName,但不调用与 SHtml.submit 关联的函数。

谢谢!

4

3 回答 3

9

This question is kind of old, but I recently needed to know this myself, and this is the best solution I've seen so far:

ajaxForm(
    bind("entry", xhtml,
         "firstName" -> text(firstName, firstName = _),
         "lastName" -> text(lastName, lastName = _),
         "submit" -> submit("Add user", processEntryAdd _),
    ) ++ hidden(processEntryAdd _)
)

By adding the processing to a hidden form element you get to keep the the submit button, without changing any view code.

You can add client side behaviour by having processEntryAdd() return a JsCmd:

def processEntryAdd() {
    Log.info("processEntryAdd: " + firstName + ", " + lastName)
    JsRaw("alert('process entry added')")
}
于 2009-09-09T22:09:09.523 回答
1

In response to this question David Pollak suggested using

"submit" -> SHtml.hidden("Add user", processEntryAdd) ++

on the lift mailing list.

于 2009-04-22T20:33:37.880 回答
1

这是答案,滚动到底部,(忽略<后面的第一个空格)

http://www.assembla.com/wiki/show/liftweb/ajaxForm

"提交" -> (SHtml.hidden(auth) ++ < input type="submit" value="Login"/>)

于 2011-01-04T14:41:11.080 回答