Jquery脚本显示条件字段onload

时间:2016-12-26 17:56:03

标签: javascript jquery html forms

我有一个表单需要添加一些条件字段。以下是关于小提琴的链接:http://jsfiddle.net/P2Ab2/59/#&togetherjs=ap7ooRf0rw

<script>
jQuery(document).ready(function ($) {
$('input[name="Payer Type"]').change(function () {
        var val1 = $("input[name='Payer Type']:checked").val();
        if (val1 == "Medicare") {
$("#Medicare_num").show("slow");
        } else {
$("#Medicare_num").hide("slow");        }

});

$('input[name="Payer Type"]').change(function () {
        var val1 = $("input[name='Payer Type']:checked").val();
        if (val1 == "Medicaid") {
$("#Medicaid_num").show("slow");
        } else {
$("#Medicaid_num").hide("slow");        }

});

//
$('input[name="Payer Type"]').change(function () {
        var val1 = $("input[name='Payer Type']:checked").val();
        if (val1 == "Commercial Insurance") {
$("#Comm_name, #Comm_policy_num").show("slow");
        } else {
$("#Comm_name, #Comm_policy_num").hide("slow");
        }
});


//

});
</script>
<form>
  <table border="0" cellpadding="1" cellspacing="1" style="width: 750px;">
    <tbody>
      <tr>
        <td style="width: 381px;">Please Select Payer Type:</td>
        <td colspan="3" style="width: 340px;">
          <input name="Payer Type" type="checkbox" value="Medicare" />Medicare&nbsp;
          <input name="Payer Type" type="checkbox" value="Medicaid" />Medicaid&nbsp;
          <input name="Payer Type" type="checkbox" value="Commercial Insurance" />Commercial Insurance</td>
      </tr>
      <tr>
        <td style="width: 381px;">&nbsp;</td>
        <td colspan="3" style="width: 340px;">
          <input id="Medicare_num" name="Medicare Num" placeholder="Medicare ID#" type="text" />
          <input id="Medicaid_num" name="Medicaid Number" placeholder="Medicaid ID #" type="text" />
          <input id="Comm_name" name="Commercial Insurance Name" placeholder="Commercial Insurance Name" size="50" type="text" />
          <input id="Comm_policy_num" name="Commercial Insurance Number" placeholder="Policy #" type="text" />
        </td>
      </tr>
    </tbody>
  </table>
</form>

现在,除了一件事之外,我有它工作:所有条件文本字段立即显示。我希望它仅在选中复选框时显示。

2 个答案:

答案 0 :(得分:0)

首先,您应该给复选框添加不同的名称,目前您无法区分它们,因为它们都具有相同的名称/ ID,之后您可以看到是否选中了复选框:

{
    "name": "Weekday_Morning_Hours_Scale",
    "capacity": {
    "minimum": "4",
    "maximum": "12",
    "default": "4"
    },
    "rules": [],
    "recurrence": {
    "frequency": "Week",
    "schedule": {
        "timeZone": "Pacific Standard Time",
        "days": [
        "Monday",
        "Tuesday",
        "Wednesday",
        "Thursday",
        "Friday"
        ],
        "hours": [
        6
        ],
        "minutes": [
        0
        ]
    }
    }
},

成为if $('#' + id).is(":checked") { //show textbox } '医疗保险'或复选框的ID。在加载时运行该id语句,如果选中该复选框,它将仅显示文本框。

答案 1 :(得分:0)

绑定单个更改事件处理程序并通过使用attribute starts with selector选择元素来切换。

if
jQuery(document).ready(function($) {
  // bind event handler to the checkbox
  $('input[name="Payer Type"]').change(function() {
    // get input element where name attribute is starts with the checkbox value
    // and then toggle the visibility based on the checked property
    $('input[name^="' + this.value + '"]').toggle(this.checked);
    // trigger the change event 
  }).change();
});