Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 如何在GET请求中传递单选按钮?_Html_Ruby_Radio Button_Sinatra - Fatal编程技术网

Html 如何在GET请求中传递单选按钮?

Html 如何在GET请求中传递单选按钮?,html,ruby,radio-button,sinatra,Html,Ruby,Radio Button,Sinatra,单选按钮如何作为参数传递?我使用的是Ruby Sinatra web服务器。如何在服务器端获取所选单选按钮的值 HTML 这只是一个通用提示,因为它是http,所以在ruby中应该是相同的 该值在名称上设置,即regRadio。由于两个单选按钮的值均为1,如果未设置,则始终会有值1或无值。因此其中一个应该是例如2。首先,方法应该是post,而不是get。剩下的似乎没问题。这对我很有用: require 'sinatra' get '/' do <<-HTML <f

单选按钮如何作为参数传递?我使用的是Ruby Sinatra web服务器。如何在服务器端获取所选单选按钮的值

HTML


这只是一个通用提示,因为它是http,所以在ruby中应该是相同的


该值在名称上设置,即regRadio。由于两个单选按钮的值均为1,如果未设置,则始终会有值1或无值。因此其中一个应该是例如2。

首先,方法应该是
post
,而不是
get
。剩下的似乎没问题。这对我很有用:

require 'sinatra'

get '/' do
  <<-HTML
    <form method="post" action="/register">
      <input type="radio" name="regRadio" value="1" />
      <input type="radio" name="regRadio" value="2" />
      <input type="radio" name="regRadio" value="3" />
      <input type="radio" name="regRadio" value="4" />
      <input type="submit" value="Register"/>
    </form>
  HTML
end

post '/register' do
  "You selected #{params[:regRadio]}"
end
需要“sinatra”
获取“/”do

在您的示例中,如果选择了任一单选按钮,它将始终发送
regRadio=1
。您可能希望为每个单选按钮使用一个不同的值,以便您可以判断实际选择了哪个单选按钮。我的错误是,单选按钮将具有不同的值。您在询问之前是否尝试了您的代码?是的,尝试访问参数[:regRadio]会一直导致0。始终是0,还是实际上为零?(也就是说,你是在给
nil
上的_i
电话吗,这会让你产生一种幻觉,以为你是零?)
get '/register' do
  params[:regRadio]??
end
require 'sinatra'

get '/' do
  <<-HTML
    <form method="post" action="/register">
      <input type="radio" name="regRadio" value="1" />
      <input type="radio" name="regRadio" value="2" />
      <input type="radio" name="regRadio" value="3" />
      <input type="radio" name="regRadio" value="4" />
      <input type="submit" value="Register"/>
    </form>
  HTML
end

post '/register' do
  "You selected #{params[:regRadio]}"
end