简体   繁体   English

有没有一种方法可以通过选中单选按钮来确定必填字段?

[英]Is there a way to determine required fields by checking a radio button?

We have consumer and business services, which customers can sign up for via a web form with the follow fields. 我们拥有消费者和企业服务,客户可以通过带有以下字段的Web表单进行注册。

Full Name: Business Name: Email Address: Phone Number: 全名:公司名称:电子邮件地址:电话号码:

Is there a way to determine required fields by checking a radio button? 有没有一种方法可以通过选中单选按钮来确定必填字段?

I would like to have Consumer and Business radio buttons. 我想使用“消费者和企业”单选按钮。 If you select the Consumer radio button, the Business Name field is not required but the rest of the fields are required. 如果选择“使用者”单选按钮,则“公司名称”字段不是必需的,但其余字段是必需的。 However, if you select the Business radio button than all fields are required including the Business Name. 但是,如果选择“业务”单选按钮,则所有字段都是必需的,包括“业务名称”。

Thanks! 谢谢!

You have 2 issues to deal with: 您有2个问题要处理:

  1. On the HTML page, showing and hiding fields according to the radio button selection. 在HTML页面上,根据单选按钮的选择显示和隐藏字段。
  2. Telling your server-side script to enforce the different required fields depending on the radio button selection. 告诉服务器端脚本根据单选按钮的选择来强制执行不同的必填字段。

For #1, you need to use JavaScript to manipulate the HTML page dynamically. 对于#1,您需要使用JavaScript动态地处理HTML页面。

jQuery is a relatively easy way to do this. jQuery是一种相对简单的方法。

For example: 例如:

<p>
   Business: <input type="radio" name="BusCons" id="Business" value="B" />
</p>
<p>
   Consumer: <input type="radio" name="BusCons" id="Consumer" value="C" />
</p>
<p>
   Business name: <input type="text" name="BusinessName" />
</p>
....
<script>
   $("#Business").click(function () {
       $("#BusinessName").show();
   });
   $("#Consumer").click(function () {
       $("#BusinessName").hide();
   });
</script>

For #2, you need a server-side script that can perform conditional field enforcement. 对于#2,您需要一个可以执行条件字段强制执行的服务器端脚本。

Tectite FormMail is one such script, using the "conditions" feature. Tectite FormMail是一种使用“条件”功能的脚本。

For example: 例如:

<input type="hidden" name="conditions" value=":@
  @IF@ BusCons ~ /B/ @
  BusinessName@
  @Please enter your business name@" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM