从单个单选按钮的选择中显示一组/选择的单选按钮的最佳方式?
我有一个表格,对于某些问题会询问是否包含某些内容以及是否不提供原因。
因此,我需要一个单选按钮,它将其值记录到数据库中,就像我使用 RadioButtonFor 设置的正常值一样,如果选择“否”(假),则将显示其他单选按钮的组/列表。
Ofc 这只是理想的解决方案,如果此方法不可行,那么另一种解决方案可能是在控制器中使用 if 语句,这样如果该主单选按钮的值为“是”(true),那么它将设置当将表单记录到数据库时,x、y 和 z 单选按钮设置为“否”(假)。
这是我关于如何获得相同的最终结果的两个想法,但对于第一个想法,我认为执行其功能的最简单方法是在 jquery 中,我对 jquery 相当陌生,所以很难想出如何做对于
第二个想法,它 1 不理想,2 我不确定如何引用这些单选按钮/代码 if 语句来执行所述任务。
任何其他想法也将受到欢迎,但希望能够帮助您实施它们。
I have a form which for some questions would ask if something was included and if it isn't to supply a reason.
So I need a radio button which records to the database it's value like normal which I have setup with a RadioButtonFor and if "No"(false) is selected then a group/list of other radiobuttons will display.
Ofc this is just the ideal solution if this method isn't feasible then another solution would be to maybe a if statement in the controller so that if that main radiobutton has a value of "Yes"(true) then it would set the values of x, y and z radiobuttons to "No"(false) when it records the form to the database.
These are the 2 ideas I have on how to get the same end result but for the 1st idea I think the easiest way to perform it's function would be in jquery which I'm fairly new at so would struggle to come up with how to do it
For the 2nd idea it's 1 not ideal and 2 I'm not sure how I would then reference those radiobuttons/code the if statement to do said task.
Any other ideas would also be welcome but hopefully with help on how to implement them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,我想出了让控制器整理验证的最简单方法,即
if (wd.AppTher == true)
{ wd.AppTherRea = 0;这是下拉列表的示例,对于另一个单选按钮,它将代替“0”或您想要的下拉列表的任何值,因为它是“false”,或者如果您想要/有多个选项int 在 db 字段类型中,那么您也只需将 N/A 或 w/e 值设置为 =,对于多个 RB 或 DDL,然后只需在 { 和 } 之间添加更多内容,wd 是分配用于表示表的值,否则可以使用视图模型值。
Well I figured out the simpliest way to get the controller to sort out the validation which was
if (wd.AppTher == true)
this is the example for a drop down and for another radiobutton it would be instead of "0" or whatever value u want for the drop down for it to be "false" or if you want/have multiple options with int at the db field type then you just set your N/A or w/e value as the = too, for multiple RB or DDL then just add more in between { and }, wd was the value assigned to represent the table and otherwise a viewmodel value could be used.{ wd.AppTherRea = 0; }
我会选择你的第一个解决方案。在页面上渲染两组单选按钮,但使用 css 隐藏第二组。然后在第一组中的单选按钮上设置一些 onclick 事件,以根据单击的单选按钮显示/隐藏第二组。
我认为由于您正在编写 onclick 处理程序,因此手动编写单选按钮的 html 会更容易,但您也可以使用 RadioButtonFor 来管理它。
因此,您的是/否将如下所示:
您的其他选项将如下所示:
在您的操作方法中,如果第一个 YesNo 为 true,则忽略任何 x,y,z 值:
I would go with your first solution. Render both groups of radio buttons on the page but hide the second group with css. Then set some onclick events on the radio buttons in your first group to show/hide the second group depending on which one is clicked.
I think it is easier to just write the html for the radio buttons by hand because of the onclick handlers you are writing, but you could probably manage it using RadioButtonFor as well.
So your yes/no would look like this:
Your other options would look like this:
In your action method ignore any x,y,z values if the first YesNo was true :
嗯,这可能听起来有些过分,但我会选择两种解决方案。从演示的角度来看,您需要 javascript 脚本端代码来正确执行此操作 - 并且还需要服务器端代码来正确执行验证。
如果您仅实现客户端验证,那么如果浏览器不支持 javascript,或者只是禁用了 javascript,系统将如何表现?您不能认为 JavaScript 支持是理所当然的...
OTOH,如果您添加了您正在谈论的客户端功能,您将提供最佳的用户体验...
以及您对如何进行服务器端验证的疑问:那就是使用 ASP.NET MVC 很容易 - 在加载时,只需设置您在发布期间阅读的相同 ViewData 条目/ViewModel 属性。
编辑 那么我们来谈谈完整的解决方案。
再说一遍,我不确定我是否理解您在这里需要什么。您正在谈论单选按钮,但您似乎也认为您将能够单独控制它们(许多单选按钮绑定到许多字段)。通常情况并非如此 - 一组单选按钮通常绑定到相同字段,每个单选按钮表示不同的值(因此与单个下拉列表完全相同)。当然,这并不意味着您的数据库必须以相同的方式运行...
请参阅此示例:
这将绑定到单个控制器参数,
favColor
,具有默认值“蓝色”的价值。请注意,为了方便起见,我们为每个单选按钮分配了一个不同的客户端 ID(rbColorBlue
、rbColorGreen
等)。这意味着您将能够在 jQuery 代码中单独处理每个单选按钮,即使它们对服务器端控制器代表单个值。谈论服务器端代码,操作将如下所示:(
当然,您也可以使用 ViewModel,但我不会在这里讨论该选项。)
回到客户端。假设您不想显示
taOtherColor
,除非用户选择rbColorOther
单选按钮。 jQuery 代码将是这样的:我想就是这样。如果我错过了什么,请告诉我...:-)
Well, this may sound overkill, but I would go with both solutions. You need the javascript script side code to do it right from a presentation standpoint - and you need the server-side code to do the validation right too.
If you implement only the client-side validation, how the system will behave if a browser has no support to javascript, or if it is just disabled? You cannot take javascript support for granted...
OTOH, you would offer a best user experience if you added that client-side functionality you're talking about...
And about your doubt of how to do the server-side validation: that's easy with ASP.NET MVC - on load, just set the same ViewData entry/ViewModel property that you have read during post.
Edit So let's talk about a complete solution.
Again, I'm not sure I understood what you need here. You're talking about radiobuttons, but you also seem to think you'll be able to control them individually (many radios binding to many fields). That's not usually the case - a group of radiobuttons is normally bound to the same field, with each radiobutton meaning a different value (so exactly like a single dropdown list). Of course, that does not mean your database must behave in the same way...
See this example:
This will bound to a single controller parameter,
favColor
, with a default value of "Blue". See that, for convenience, we're assigning a distinct client-side id for each radiobutton (rbColorBlue
,rbColorGreen
and so forth). That means that you'll be able to treat each radiobutton individually in your jQuery code, even if they represent a single value to the server-side controller.Talking about the server-side code, that's how the action will look like:
(Of course, you could also work with a ViewModel, but I'll not talk about that option here.)
Back to client-side. Let's suppose you don't want to show
taOtherColor
, unless the user selects therbColorOther
radiobutton. The jQuery code would be something like this:I guess that would be it. Let me know if I have missed something... :-)