javascript 处理表单加载的代码

javascript code to work on form load

希望我现在清楚我的问题了。

我有一个共享点列表,其中 hide/show 某些字段基于另一个字段中的数据(Yes/No 下拉列表)。例如,我的第 1 列带有是和否下拉列表。如果此列 1 中的值选择为是,则应显示 A、B、C 和 D 四列。如果第 1 列为否,则应隐藏 A、B、C 和 D。它在加载表单时运行良好。

但是,当表单 returns 在保存时出错时,在依赖字段中选择 "Yes" 时显示的字段将被隐藏,即使依赖字段仍然是 "Yes" 并且我必须在“是”和“否”选项之间切换才能再次显示这些字段。

下面是我用过的代码。

<script src="https://code.jquery.com/jquery-1.7.2.min.js" type=text/javascript></script>
<script type="text/javascript">
// Execute the following JavaScript after the page has fully loaded, when it's ".ready"
$(document).ready(function() {
  $('nobr:contains("Question 1")').closest('tr').hide();
  $('nobr:contains("Question 2")').closest('tr').hide();
  $('nobr:contains("Question 3")').closest('tr').hide();
  $('nobr:contains("Question 4")').closest('tr').hide();

  //Show/hide columns based on Drop Down Selection 
  $("select[title='Do you know Java Scripting']").change(function() {
    if ($("select[title='Do you know Java Scripting']").val() != "Yes") {
      $('nobr:contains("Question 1")').closest('tr').hide();
      $('nobr:contains("Question 2")').closest('tr').hide();
      $('nobr:contains("Question 3")').closest('tr').hide();
      $('nobr:contains("Question 4")').closest('tr').hide();
    } else {
      $('nobr:contains("Question 1")').closest('tr').show();
      $('nobr:contains("Question 2")').closest('tr').show();
      $('nobr:contains("Question 3")').closest('tr').show();
      $('nobr:contains("Question 4")').closest('tr').show();
    }
  });
});

您在 change 事件中有您的 if () 声明。您需要在事件之外进行类似的检查,以便每次加载页面时执行检查并相应地显示或隐藏列。