Javascript ZF2-无法隐藏Zend\Element\select上的选择选项
我正在研究ZF2,并试图使用javascript设置两个相关下拉列表。当firest更改时,我开始尝试隐藏第二个select字段中的所有选项 这是我的表格:Javascript ZF2-无法隐藏Zend\Element\select上的选择选项,javascript,zend-framework2,zend-form,Javascript,Zend Framework2,Zend Form,我正在研究ZF2,并试图使用javascript设置两个相关下拉列表。当firest更改时,我开始尝试隐藏第二个select字段中的所有选项 这是我的表格: $this->add(array( 'type' => 'Zend\Form\Element\Select', 'name' => 'category_list', 'options' => array(
$this->add(array(
'type' => 'Zend\Form\Element\Select',
'name' => 'category_list',
'options' => array(
'label' => 'Event category ',
'style' => 'display:none;',
'value_options' => array(
),
),
'attributes'=> array(
'id'=>'list1',
'onchange'=>'hide()'
)
));
$this->add(array(
'type' => 'Zend\Form\Element\Select',
'name' => 'subcateg_list',
'options' => array(
'label' => 'Type Incident ',
'style' => 'display:none;',
'value_options' => array(
)),
'attributes'=> array(
'id'=>'list2'
)
));
请注意,选择字段是在控制器类上填充的
这是我的Javascript函数:
function hide()
{
var op = document.getElementById("list2").getElementsByTagName("option");
for (var i = 0; i < op.length; i++) {
? op[i].disabled = true
: op[i].disabled = false ;
}
}
但当第一个select字段更改时,没有任何更改。那么问题出在哪里呢?您的代码应该抛出语法错误。三值运算器?:需要检查一个条件。 见: 正如Fouad Fodail建议的->一个jQuery示例:
function hide()
{
$('#list2 option').each(function() {
$(this).prop('disabled', condition);
// ^-- Do not forget to change this
});
}
注意:Internet Explorer 7和早期版本不支持标记的disabled属性。
另一个示例是,如果第一个已更改,则这将禁用第二个:
$('#select1').change(function() {
$('#select2').prop('disabled', true);
});
未经测试
文件
您的代码应该抛出语法错误。三值运算器?:需要检查一个条件。 见: 正如Fouad Fodail建议的->一个jQuery示例:
function hide()
{
$('#list2 option').each(function() {
$(this).prop('disabled', condition);
// ^-- Do not forget to change this
});
}
注意:Internet Explorer 7和早期版本不支持标记的disabled属性。
另一个示例是,如果第一个已更改,则这将禁用第二个:
$('#select1').change(function() {
$('#select2').prop('disabled', true);
});
未经测试
文件
问题出在for循环中的js代码中。尝试使用jQuery而不是本机JavaScript。这里有没有关于如何使用jQuery的例子?问题在于for循环中的js代码。尝试使用jQuery而不是本机JavaScript。这将更加简单和干净。这里有关于如何使用jQuery的例子吗?