javascript - 反应表单中带有单选按钮的条件字段是否有效?
问题描述
我正在创建一个带有“职业”字段作为单选按钮的表单。职业有两种选择——自雇和服务。根据用户对职业的选择,我显示了表单的不同字段。我正在使用 if-else 进行此条件渲染。代码适用于 if 部分,但不适用于 else 部分。我附上我的代码以供参考。
<div className="row">
<div className="col-md-3">
<input
type= "radio"
id= "fatherService"
name= "fatherOccupation"
value = "service"
checked={fatherOccupation === "service"}
onChange={handleFatherOccupation}
/>
<label htmlFor="fatherService">Service</label>
</div>
<div className="col-md-6">
<input
type= "radio"
id= "fatherSelfEmployed"
name= "fatherOccupation"
value = "selfEmployed"
checked={fatherOccupation === "selfEmployed"}
onChange={handleFatherOccupation}
/>
<label htmlFor="fatherSelfEmployed">Self Employed</label>
</div>
</div>
</div>
</div>
{fatherOccupation === "service" ? (
<div className="row">
<div className="col-md-6">
<label>Company Name:</label>
<input
type="text"
className="form-control p-2"
id="fatherCompanyName"
name="fatherCompanyName"
{...register("fatherCompanyName", {
required:"Please enter father's company name",
})}
/>
</div>
</div>
) : (
<div className="row">
<div className="col-md-6">
<label>Type of Business:</label>
<input
type="text"
className="form-control p-2"
id="fatherTypeOfBusiness"
name="fatherTypeOfBusiness"
{...register("fatherTypeOfBusiness", {
required:"Please enter father's type of business",
})}
/>
</div>
</div>
)}
解决方案
推荐阅读
- r - 使用函数更改 R 中的单元格名称
- angular - 从键值枚举中填充 ng-select 项目
- java - java.io.IOException:下载文件时管道损坏
- laravel - 将多个数组传递给 Laravel 8 中的视图
- c++ - “AddCustomer”未在此范围内声明。错误在哪里?
- c++ - 矩阵大小和循环变量的正确类型
- tensorflow - 如何克服错误的尺寸问题
- vue.js - 我的 Vue“wheel.vue”组件有什么问题?当它呈现为 html 时,它显示为空?
- google-apps-script - 如何在列中生成下一个可用的点十进制符号数
- python - 如何在opencv中替换部分图像?