简体   繁体   English

如何在Zend Framework中使用ViewHelper设置Select lke disabled ='disabled'的任何属性

[英]How to set any attribute of Select lke disabled='disabled' by using ViewHelper in Zend Framework

I am using Zend Frameworks ViewHelpers. 我正在使用Zend Frameworks ViewHelpers。 I am trying to pass something to set disabled attribute in SELECT. 我试图传递一些东西来设置SELECT中的禁用属性。 For example if 例如,如果

$countries = array(1=>'Select Option', 2=>'us', 3=>'uk')

and formSelect('country','us',null,$this->countries) formSelect('country','us',null,$this->countries)

I need to diable first option ie 'Select Option' 我需要先拨打第一个选项,即“选择选项”

Do you have any idea ? 你有什么主意吗 ?

Thanks in addvance 谢谢你的补充

I don't think you can disable one element? 我不认为你可以禁用一个元素? If you disable it then why have it at all? 如果你禁用它然后为什么要它?

You can only disable the whole <select> input. 您只能禁用整个<select>输入。

Suggest you write validation to not accept the first element. 建议您编写验证以不接受第一个元素。

Edit after OP's comment about being able to do this OP关于能够做到这一点的评论后编辑

Here is another answer 这是另一个答案

// Get the countries element (do this after adding your options), then set the 
// attribute disable for option '1'
$form->getElement("countries")->setAttrib("disable", array(1));

This is suggested here 这个建议在这里

There IS a way of doing it through Zend_Form (at least on my current ve 1.11): 有一种方法可以通过Zend_Form来实现(至少在我目前的1.11版本中):

$this->addElement
(
    "select","selectName", 
    array("multiOptions"=>array("one","two","three"), "disable"=>array(0,1))
);

That one will disable first two options. 那个将禁用前两个选项。

Credit goes to jakenoble. 信用归于jakenoble。
Just reformatted the code to use the formSelect-viewhelper instead of a form-element. 只需重新格式化代码即可使用formSelect-viewhelper而不是form-element。

<?php
$countries = array(1 => 'Select Option', 2 => 'us', 3 =>'uk');
echo $this->formSelect('country', 2, array('disable' => array(1)), $countries)

This will result in: 这将导致:

<select name="country" id="country"> 
    <option value="1" label="Select Option" disabled="disabled">Select Option</option> 
    <option value="2" label="us" selected="selected">us</option> 
    <option value="3" label="uk">uk</option> 
</select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM