我有一个带有一堆字段的 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 开发人员,我继承了这个站点。只是想解决最后几个错误,这样我们就可以上线了
- 字段比上面列出的要多,我只是给了你一些适用的字段,这样你就可以了解我的表单是如何设置的要点
- 开发中表单的当前网址: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),
最佳答案
好的,我找到了解决方案。希望这会对与我有类似情况的人有所帮助。
':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"),
关于php - Drupal 7 Forms API 条件逻辑在 IE 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9794148/