get checkbox and radio button value in lift

北城余情 提交于 2019-12-23 22:58:02

问题


i am trying to processing a form in lift frame work. my form is having one checkbox and radiobuttons. how could i check whether the checkbox is checked or not and the selected radio button.the following code i used to get other elements value.

the view:

<form class="lift:MySnippet?form=post">
 From:<input type="text" name="source" /><br />
 To:  <input type="text" name="destination" /><br />
 Age: <input type="text" name = "age"/><br />
 Return: <input type="checkbox" name="needreturn" value="Return Ticket" /><br />
 <input type="radio" name="sex" value="male" />Male<br />
 <input type="radio" name="sex" value="male" />Female<br />
 <input type="submit" value="Book Ticket"/>
</form>

and MySnippet scala code is:

  object MySnippet {
     def render = {
             var from = ""
             var to = ""
             var age = 0

            def process() {
                S.notice("in process function")
            }

     "name=source" #> SHtml.onSubmit(from = _) &
     "name=destination" #> SHtml.onSubmit(to = _) &
     "name=age" #> SHtml.onSubmit(s => asInt(s).foreach(age = _)) &
     "type=submit" #> SHtml.onSubmitUnit(process)        
   }
  }

in this how could i process the checkbox and radio button. can anyone help me...thanx in advance.


回答1:


Do you need to specify the choices in your HTML? If not, the easiest way is:

Return: <input type="checkbox" name="needreturn" /><br />
Sex: <input type="radio" name="sex" />

and the CSS Transform:

val radioChoices = List("male", "female")
var sex:Box[String] = None
var needReturn:Boolean = false

"@sex" #> SHtml.radio(radioChoices, sex, (resp) => sex = Full(resp)) &
"@needreturn" #> SHtml.checkbox(needReturn, (resp) => needReturn = resp)

You could replace SHtml.radio with SHtml.ajaxRadio and SHtml.checkbox with SHtml.ajaxCheckbox if you want your selection to be sent to the server every time the value is changed, instead of when the form is submitted

I believe you can also use the SHtml.onSubmit as you do above for the checkbox and radio, but I'd have to do some testing to figure out exactly how.

With regards to the radio button, you can find some information about changing the way the label is output here if you need to: https://groups.google.com/forum/?fromgroups=#!topic/liftweb/rowpmIDbQAE




回答2:


Use SHtml.checkbox, SHtml.radio

By the way, the <input>-s should be SHtml.text, I think. So, they're not buttons -- they're inputs. Don't forget to check the resulting html in the web page with firebug. (You'd see that using the current code you have input=text deleted.)



来源:https://stackoverflow.com/questions/15879991/get-checkbox-and-radio-button-value-in-lift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!