在Joomla中编写PHP的JavaScript代码

时间:2011-05-31 16:47:42

标签: php javascript dynamic joomla

我有以下问题:

我希望用户能够通过Joomla后端上传图片。

我使其与“媒体”字段非常相似,但是添加字段是每当用户使用iframe内的媒体组件(与媒体字段相同)选择图像时,使用javascript将新字段插入到字段集中。

该新字段具有所选的图像值,因此您可以更改或删除它。

当编辑布局加载时,PHP会生成包含之前添加的图像的所有字段,I.E。 :

|image1.png| change  delete
|image2.png| change  delete
|          | addNew

PHP生成的那些更改按钮可以很好地工作,因此当您单击它们时,会弹出iframe并让您选择另一个图像。删除按钮也有效(擦除字段)

然后我点击addNew并选择另一张图片:

|image1.png| change  delete
|image2.png| change  delete
|image3.png| change  delete
|          | addNew

它似乎有效,但是当我点击“更改”时,浏览器会将我重定向到组件:S,而不是获取弹出式iframe。

我看了HTML的源代码,PHP和javascript生成的字段都是一样的,所以问题必须是一些javascript。

这是字段的PHP HTML:

        $html = array ( );

        $html[] = '<li id="'.$id.'">';
        $html[] =   '<label></label>';
        $html[] =   '<input'.
                        ' type="text" readonly="true"'.
                        ' value="' .$value. '"'.
                        ' id="'.$id.'_data"'.
                        ' size="60"'.
                    '>';
        $html[] =   '<div class="button2-left">';
        $html[] =       '<div class="blank">';
        $html[] =           '<a href="index.php?option=com_media&amp;view=images&amp;tmpl=component&amp;asset=&amp;author=&amp;fieldid='.$id.'_data&amp;folder='.$this->folder.'"'.
                                'class="modal"'.
                                'rel="{handler: \'iframe\', size: {x: 800, y: 500}}"'.          
                            '>';
        $html[] =               'Cambiar';
        $html[] =           '</a>';
        $html[] =       '</div>';
        $html[] =   '</div>';
        $html[] =   '<div class="button2-left">';
        $html[] =       '<div class="blank">';
        $html[] =           '<a'.
                                ' onclick="document.getElementById(\''.$id.'_data\').value=\'\'; document.getElementById(\''.$id.'\').style.display=\'none\'; document.getElementById(\''.$id.'\').onchange();"'.
                                ' href="#"'.
                            '>';
        $html[] =               'Eliminar';
        $html[] =           '</a>';
        $html[] =       '</div>';
        $html[] =   '</div>';
        $html[] = '</li>';

这是用PHP编写的javascript

        $script2 = array();
        $script2[] = '  function new_'.$this->id.'( value ) {';
        $script2[] = '      var container = document.getElementById(\''.$this->id.'_container\');';

        $script2[] = '      var child  = document.createElement(\'li\');';
        $script2[] = '      var lbl    = document.createElement(\'label\');';
        $script2[] = '      var input  = document.createElement(\'input\');';
        $script2[] = '      var but    = document.createElement(\'div\');';
        $script2[] = '      var but2   = document.createElement(\'div\');';
        $script2[] = '      var blank  = document.createElement(\'div\');';
        $script2[] = '      var blank2 = document.createElement(\'div\');';
        $script2[] = '      var add    = document.createElement(\'a\');';
        $script2[] = '      var clear  = document.createElement(\'a\');';

        $script2[] = '      child.setAttribute( \'id\' , \''.$id_insert.' );';

        $script2[] = '      input.setAttribute ( \'type\' , \'text\' );';
        $script2[] = '      input.setAttribute ( \'readonly\' , \'true\' );';
        $script2[] = '      input.setAttribute ( \'id\' , \''.$id_insert.'+\'_data\' );';
        $script2[] = '      input.setAttribute ( \'size\' , \'60\' );';
        $script2[] = '      input.setAttribute ( \'value\' , value );';

        $script2[] = '      but.setAttribute  ( \'class\' , \'button2-left\' );';
        $script2[] = '      but2.setAttribute ( \'class\' , \'button2-left\' );';

        $script2[] = '      blank.setAttribute  ( \'class\' , \'blank\' );';
        $script2[] = '      blank2.setAttribute ( \'class\' , \'blank\' );';

        $script2[] = '      add.setAttribute   ( \'class\' , \'modal\');';
        $script2[] = "      add.setAttribute   ( 'rel' , \"{handler: 'iframe', size: {x: 800, y: 500}}\");";
        $script2[] = "      add.setAttribute   ( 'href' , 'index.php?option=com_media&view=images&tmpl=component&asset=&author=&fieldid=".$id_insert."+'_data&folder=".$folder_secure."' );";
        $script2[] = '      add.innerHTML = \'Cambiar\';';

        $script2[] = '      clear.setAttribute ( \'href\' , \'#'.$this->id.'_create_data\' );';
        $script2[] = '      clear.setAttribute ( \'onclick\' , \'document.getElementById("'.$id_insert.'+\'_data").value=""; document.getElementById("'.$id_insert.'+\'").style.display="none"; document.getElementById("'.$id_insert.'+\'").onchange();\' );';
        $script2[] = '      clear.innerHTML = \'Eliminar\';';

        $script2[] = '      child.appendChild ( lbl );';
        $script2[] = '      child.appendChild ( input );';

        $script2[] = '      child.appendChild ( but );';
        $script2[] = '      but.appendChild ( blank );';
        $script2[] = '      blank.appendChild ( add );';

        $script2[] = '      child.appendChild ( but2 );';
        $script2[] = '      but2.appendChild ( blank2 );';
        $script2[] = '      blank2.appendChild ( clear );';

        $script2[] = '      var add_new = document.getElementById(\''.$this->id.'_create_new\');';
        $script2[] = '      container.removeChild ( add_new );';

        $script2[] = '      container.appendChild ( child );';
        $script2[] = '      container.appendChild ( add_new );';

        $script2[] = '      document.getElementById(\''.$this->id.'_data\').value = \'\';';
        $script2[] = '      '.$count_insert.'++;';

        $script2[] = '      }';

浏览器在javascript中没有发现任何错误,所以我不知道它为什么不起作用

0 个答案:

没有答案