【问题标题】:Drupal 7 Forms API Conditional Logic not working in IEDrupal 7 Forms API 条件逻辑在 IE 中不起作用
【发布时间】:2012-03-20 20:09:13
【问题描述】:

我有一个带有一堆字段的 drupal 7 表单:

$form['account_type'] = array(
  '#title' => t('Utility Account Type'),
  '#type' => 'select',
  '#options' => necp_enrollment_administration_portal_account_type_options(),
  '#required' => TRUE,
  '#default_value' => isset($form_state['values']['account_type']) ? $form_state['values']['account_type'] : '',
);

// Should show if account_type = 1
$form['home_wrapper'] = array(
  '#type' => 'fieldset',
  '#states' => array(
    'visible' => array(
      ':input[name="account_type"]' => array('value' => 1),
    ),
  ),
);
$form['home_wrapper']['first_name_1'] = array(
  '#title' => t('Primary Account First Name'),
  '#type' => 'textfield',
  '#default_value' => isset($form_state['values']['first_name_1']) ? $form_state['values']['first_name_1'] : '',
  '#states' => array(
    'required' => array(
      ':input[name="account_type"]' => array('value' => 1),
    ),
  ),
);
$form['home_wrapper']['last_name_1'] = array(
  '#title' => t('Primary Account Last Name'),
  '#type' => 'textfield',
  '#default_value' => isset($form_state['values']['last_name_1']) ? $form_state['values']['last_name_1'] : '',
  '#states' => array(
    'required' => array(
      ':input[name="account_type"]' => array('value' => 1),
    ),
  ),
);

// Should show if account_type = 2
$form['business_wrapper'] = array(
  '#type' => 'fieldset',
  '#states' => array(
    'visible' => array(
      ':input[name="account_type"]' => array('value' => 2),
    ),
  ),
);
$form['business_wrapper']['company_name'] = array(
  '#title' => t('Company/Organization'),
  '#type' => 'textfield',
  '#default_value' => isset($form_state['values']['company_name']) ? $form_state['values']['company_name'] : '',
  '#states' => array(
    'required' => array(
      ':input[name="account_type"]' => array('value' => 2),
    ),
  ),
);

在 Firefox/Chrome/Opera 所有版本中,此表单的行为都应如此。然而,在所有版本的 IE 中,表单初始化为 display:none;无论 account_type 中的值是什么,所有条件字段的样式都是如此。更改 account_type 的选定选项不会影响隐藏状态。

任何调试此表单的技巧都很棒。

注意事项:

  • 我不是一个 Drupal 开发人员,我继承了这个站点。只是想解决最后几个错误,以便我们可以上线
  • 字段比上面列出的要多,我只是给了你一些适用的字段,这样你就可以了解我的表单是如何设置的
  • 正在开发的表单的当前 URL:https://northeastcleanpower.com/enroll_new
  • 我正在使用http://www.browserstack.com/ 来调试 IE 7 - 10pp4(不过我认为我们只需要支持 8 及更高版本)

我也试过了:

  • ':select[name="account_type"]' => array('value' => 1),
  • '#edit-account-type' => array('value' => 1),

【问题讨论】:

    标签: php drupal drupal-7 drupal-modules drupal-forms


    【解决方案1】:

    好的,我找到了解决方案。希望这会帮助与我情况相似的人。

    ':input[name="account_type"]' => array('value' => 1),
    

    需要:

    ':input[name="account_type"]' => array('value' => "1"),
    

    显然,IE 中的 javascript 正在评估文字类型/值而不仅仅是值:1 !== "1" 等。一旦我解决了这个问题,它就开始像冠军一样工作了。

    我的代码中还有另一个实例,其中值来自 PHP 变量,它不会接受该变量作为 int,而是接受它作为字符串...

    ':input[name="account_type"]' => array('value' => "$id"),
    

    【讨论】:

    • 你这个男人!我有$form['field_movie_credit'][$lang][$key]['field_movie_credit_character']['#states']['visible'] = array( ':input[name="field_movie_credit[' . $lang . '][' . $key . '][field_movie_credit_role][' . $lang . ']"]' => array('value' => $character_role_id), 并将其更改为$form['field_movie_credit'][$lang][$key]['field_movie_credit_character']['#states']['visible'] = array( ':input[name="field_movie_credit[' . $lang . '][' . $key . '][field_movie_credit_role][' . $lang . ']"]' => array('value' => "$character_role_id"), 和BOOM!谢谢!
    【解决方案2】:

    我的猜测是 jQuery 输入选择器由于某种原因而失败。你能尝试用#edit-account-type替换:input[name="account_type"]

    【讨论】:

    • 这个好像不行,我也试过#edit-account-type option:selected
    • 在这种情况下,可能选择器甚至没有运行。当我有时间时,我会寻找执行状态的特定行为,以便我可以看到在哪里放置换行符。
    • 感谢您的帮助,感谢您抽出时间提供帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-04
    • 2014-10-05
    相关资源
    最近更新 更多