如果我有两个选项的代码,单选按钮的结果将无法正常工作

Result of the radio buttons does not work properly if I have the code for both of the options

本文关键字:结果 工作 常工作 单选按钮 代码 两个 如果 选项      更新时间:2023-09-26

我有一个问题,我有两个单选按钮,如果您在医生单选按钮上选择,它将显示需要为医生显示的所有下一个字段,如果您选择患者单选按钮,我们希望发生同样的情况,例如,如果我有患者的代码,我这样做可以正常工作,但是如果我也为医生编写代码,它会破坏两个选项的结果这是代码...

<html>
<head>
<script src="jquery-1.11.2.min.js"></script>
</head> 
<body>
<fieldset>
    <legend>Registration Form</legend>
    <label>First Name
        <input type="text" name="fname" required="required" />
    </label>
    <br/>
    <br/>
    <label>Last Name
        <input type="text" name="lname" required="required" />
    </label>
    <br />
    <br />
    <label>Username
        <input type="text" name="username" required="required" />
    </label>
    <br />
    <br />
    <label>Email
        <input type="text" name="email" required="required" />
    </label>
    <br />
    <br />
    <label>Password
        <input type="text" name="password" required="required" />
    </label>
    <br/><br/>
 User Type:
<br/>
Doctor <input type="radio" name="answerswer" value="Doctor" />
Patient <input type="radio" name="answerswer" value="Patient" />
<br/>
<br/>
<!--DOCTOR OPTIONS 
<label style="display:none;" id="Male">Male</label>
    <input style="display:none;" type="radio" name="DoctorG" value="male" id="DoctorGM">
<label style="display:none;" id="Female">Female</label>
    <input style="display:none;" type="radio" name="DoctorG" value="male" id="DoctorGF">
<br/>
<br/>
<label style="display:none;" id="Age">Age:</label>
<input style="display:none;" type="text" name="DoctorAge" id="DoctorAge" />
<br/>
<br/>
<label style="display:none;" id="Specialty">Specialty:</label>
<select style="display:none;" id="SelectSpecialty">
  <option value="A">A</option>
  <option value="B">B</option>
  <option value="C">C</option>
  <option value="D">D</option>
</select>
<br/>
<br/>
<label style="display:none;" id="ID">Doctor ID:</label>
<input style="display:none;" type="text" name="Doctor_ID" id="Doctor_ID" />
-->
<!--PATIENT OPTIONS -->
<label style="display:none;" id="Male">Male</label>
    <input style="display:none;" type="radio" name="PatientGender" value="male" id="PatientGM">
<label style="display:none;" id="Female">Female</label>
    <input style="display:none;" type="radio" name="PatientGender" value="male" id="PatientGF">
<br/>
<br/>
<label style="display:none;" id="Age">Age:</label>
<input style="display:none;" type="text" name="PatientAge" id="PatientAge" />
<br/>
<br/>
<label style="display:none;" id="Disease">Disease:</label>
<select style="display:none;" id="SelectDisease">
  <option value="A">A</option>
  <option value="B">B</option>
  <option value="C">C</option>
  <option value="D">D</option>
</select>
<br/>
<br/>
<label style="display:none;" id="SPID">SPID:</label>
<input style="display:none;" type="text" name="PatientSPID" id="PatientSPID" />
</fieldset>
</body>
<script>
$("input[type='radio'][name='answer']").change(function () {
    if ($(this).val() == "Doctor") {
        $("#DoctorGM").show();
        $("#DoctorGF").show();
        $("#DoctorAge").show();
        $("#SelectSpecialty").show();
        $("#Male").show();
        $("#Female").show();
        $("#Age").show();
        $("#Disease").show();
        $("#ID").show();
        $("#Doctor_ID").show();
    } else {
        $("#PatientGM").hide();
        $("#PatientGF").hide();
        $("#PatientAge").hide();
        $("#SelectDisease").hide();
        $("#Male").hide();
        $("#Female").hide();
        $("#Age").hide();
        $("#Disease").hide();
        $("#ID").hide();
        $("#Doctor_ID").hide();
    }
    if ($(this).val() == "Patient") {
        $("#PatientGM").show();
        $("#PatientGF").show();
        $("#PatientAge").show();
        $("#SelectDisease").show();
        $("#Male").show();
        $("#Female").show();
        $("#Age").show();
        $("#Disease").show();
        $("#SPID").show();
        $("#PatientSPID").show();
    } else {
        $("#PatientGM").hide();
        $("#PatientGF").hide();
        $("#PatientAge").hide();
        $("#SelectDisease").hide();
        $("#Male").hide();
        $("#Female").hide();
        $("#Age").hide();
        $("#Disease").hide();
        $("#SPID").hide();
        $("#PatientSPID").hide();
    }
});</script>
</html>

PS 1:我为医生注释了代码,以便为患者正常工作PS 2:http://jsfiddle.net/niklakis/qp7s409a/24/这里有一个链接,看看如果 A 同时拥有两个代码会发生什么

假设您可以编辑此表单的 HTML,您应该将额外的字段放在单独的 div 中并仅隐藏这些div。然后,你的jQuery也变得简单得多。

现在在这个jQuery中,我们要做的是默认隐藏两个"扩展"部分。这就是$("[id^=expand]").hide()的台词.这样做是选择idexpand开头的所有元素并隐藏它们。

然后,我们选择与单击的收音机关联的扩展部分并显示它。

$("input[type='radio'][name='answer']").change(function() {
  $("[id^=expand]").hide();
  $("#expand" + $(this).val()).show();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<fieldset>
  User Type:
  <br/>Doctor
  <input type="radio" name="answerswer" value="Doctor" />
  Patient
  <input type="radio" name="answerswer" value="Patient" />
  <!--DOCTOR OPTIONS -->
  <div id="expandDoctor" style="display:none;">
    <label id="Male">Male</label>
    <input type="radio" name="DoctorG" value="male" id="DoctorG">
    <label id="Female">Female</label>
    <input type="radio" name="DoctorG" value="male" id="DoctorG">
    <br/>
    <br/>
    <label id="Age">Age:</label>
    <input type="text" name="DoctorAge" id="DoctorAge" />
    <br/>
    <br/>
    <label id="Specialty">Specialty:</label>
    <select id="SelectSpecialty">
      <option value="A">A</option>
      <option value="B">B</option>
      <option value="C">C</option>
      <option value="D">D</option>
    </select>
    <br/>
    <br/>
    <label id="ID">Doctor ID:</label>
    <input type="text" name="Doctor_ID" id="Doctor_ID" />
  </div>
  <!--PATIENT OPTIONS -->
  <div id="expandPatient" style="display:none;">
    <label id="Male">Male</label>
    <input type="radio" name="PatientG" value="male" id="PatientGM">
    <label id="Female">Female</label>
    <input type="radio" name="PatientG" value="male" id="PatientGF">
    <br/>
    <br/>
    <label id="Age">Age:</label>
    <input type="text" name="PatientAge" id="PatientAge" />
    <br/>
    <br/>
    <label id="Disease">Disease:</label>
    <select id="SelectDisease">
      <option value="A">A</option>
      <option value="B">B</option>
      <option value="C">C</option>
      <option value="D">D</option>
    </select>
    <br/>
    <br/>
    <label id="SPID">SPID:</label>
    <input type="text" name="PatientSPID" id="PatientSPID" />
  </div>
</fieldset>

但是,您

遇到了问题,因为您试图显示和隐藏实际上不存在的元素。例如,您尝试显示/隐藏不存在的#DoctorGM,您只有#DoctorG。您也应该更改这些id,因为让任何元素在 HTML 中共享相同的id都是无效的。

相关文章: