Jquery / Zend切换元素禁用不起作用

时间:2011-07-22 16:08:36

标签: javascript jquery zend-framework

我有一个Zend表单,有两个复选框,如果第一个未选中,则应禁用第二个。

这两个要素是:

    $element = $this->createElement('checkbox','show_assessments');
    $element->setLabel('Do you want to view assessments?');
    $element->setAttrib('onchange', 'javascript:toggleElement("show_assessments", "show_expired")');
    $element->clearDecorators();
    $element->addDecorator('StandardTable');
    if($preferences['show_assessments'])
        $element->setValue(1);
    $this->addElement($element);
    $this->_elementNames[] = 'show_assessments';

    $element = $this->createElement('checkbox','show_expired');
    $element->setLabel('Include expired programs?');
    if(!$preferences['show_assessments'])
        $element->setAttrib('disable', true);
    $element->clearDecorators();
    $element->addDecorator('StandardTable');
    if($preferences['show_expired'])
        $element->setValue(1);
    $this->addElement($element);
    $this->_elementNames[] = 'show_expired';

和我切换元素的Javascript函数是:

    function toggleElement(sender, receiver)
    {
        if (!sender || !receiver) return;
            if ($('#'+sender).is(':checked'))
            {
                    $('#'+receiver+' :input').removeAttr('disabled');
                    alert(receiver+' is now enabled');
            }
            else
            {
                    $('#'+receiver+' :input').attr('disabled', true);
                    alert(receiver+' is now disabled');
            }
            if ($('#'+receiver).is('checked'))
                    alert(receiver+' is checked');
            else
                    alert(receiver+' is not checked');
    } 

问题是,检查接收器是否被检查的最后一个if语句无论如何都是假的,并且接收器永远不会被切换。有什么想法吗?

编辑:

第二个元素似乎总是有问题。我试过反转它,所以show_expired是发送者,show_assessments是接收者,这次它可以判断show_expired是什么时候被检查,但是无法告诉show_assessments。

2 个答案:

答案 0 :(得分:1)

可能是你的javascript中有拼写错误:

function toggleElement(sender, receiver)
{
    if (!sender || !receiver) return;
        if ($('#'+sender).is(':checked'))
        {
                $('#'+receiver+' :input').attr('disabled', false); // <-- use attr('disabled', false instead)
                alert(receiver+' is now enabled');
        }
        else
        {
                $('#'+receiver+' :input').attr('disabled', true);
                alert(receiver+' is now disabled');
        }
        if ($('#'+receiver).is(':checked')) // < -- should be .is(':checked')
                alert(receiver+' is checked');
        else
                alert(receiver+' is not checked');
} 

第二个IF语句在'checked'之前错误地错过了冒号。

答案 1 :(得分:1)

没有jQuery就可以使用

function toggleElement(sender, receiver)
{
var sendEl = document.getElementById(sender);
var recEl = document.getElementById(receiver);
if (sendEl.checked)
    recEl.disabled = false;
else
    recEl.disabled = true;
}