2012-03-08 84 views
-1

我在php文件裏有這段代碼。它應該根據特定參數顯示並隱藏一段html內容(我有一組複選框,如果你檢查它們,根據它的複選框顯示或隱藏一些內容)。這個javascript函數沒有定義?我該如何解決它?

現在,在我的樣式表出現許多錯誤之後,在剝離它之後,我仍然得到這個:firefox錯誤控制檯中的「隱藏未定義」(我已經嘗試了螢火蟲中的錯誤控制檯,並顯示沒有)。

這是代碼,我不知道如何隱藏,可以不定義:

$script = 
    $("input[id^=\'edit-field-tipos-unidades-disponible-value\']").change(function(){' . 
    $(".fila-matriz-gastos").hide();' . 
    $(".fila-matriz-gastos").prev("label").hide();' . 
    $("input[id^=\'edit-field-tipos-unidades-disponible-value\'][checked]").each(function(){' . 
     var tipo = $(this).val();' . 
     $(".tipo-uf-" + tipo).show();' . 
     $(".tipo-uf-" + tipo).prev().show();' . 
     $("input.tipo-uf-"+tipo+"[value=-1]").click(function(){' . 
     $("input.tipo-uf-" + tipo).attr("checked", $(this).attr("checked"));' . 
     });' . 
    });' . 
    }).change();'; 

I'm幾乎在這裏的損失,因爲我想主題的輸出,但我不不瞭解JavaScript的工作方式。

謝謝!

UPDATE:

這是隱藏或顯示輸出:

<fieldset class=" collapsible"> 
<legend> 
<a href="#">Matriz de Gastos</a> 
</legend> 
<div> 
<div class="form-item"> 
<label style="display: none;"> 
Gastos por 
<em>Departamento</em> 
: 
</label> 
<div class="form-checkboxes tipo-uf-131 fila-matriz-gastos hidden" style="display: none;"> 
<div id="edit-matriz-gastos-131--1-wrapper" class="form-item"> 
<label class="option" for="edit-matriz-gastos-131--1"> 
<input id="edit-matriz-gastos-131--1" class="form-checkbox tipo-uf-131 fila-matriz-gastos hidden" type="checkbox" value="-1" name="matriz_gastos[131][-1]" style="display: none;"> 
Seleccionar/deseleccionar todos 
</label> 
</div> 
<div id="edit-matriz-gastos-131-108-wrapper" class="form-item"> 
<label class="checkbox" for="edit-matriz-gastos-131-108"> 
<input id="edit-matriz-gastos-131-108" class="form-checkbox tipo-uf-131 fila-matriz-gastos hidden" type="checkbox" value="108" name="matriz_gastos[131][108]"> 
Gas 
</label> 
</div> 

這是被隱藏或顯示的東西複選框,按點擊與否:

經過:

<label style="display: block;"><label style="display: block;"> 
Gastos por 
<em>Departamento</em> 
: 
</label> 
<div class="form-checkboxes tipo-uf-131 fila-matriz-gastos hidden" style="display: block;"> 

未選中:

<label style="display: none;"> 
Gastos por 
<em>Departamento</em> 
: 
</label> 
<div class="form-checkboxes tipo-uf-131 fila-matriz-gastos hidden" style="display: none;"> 

和其他更新:

Here's什麼從來就發現在jQuery docs

隱藏.hide()函數 - DOM操作

此功能隱藏匹配的元素在頁面上。它所做的是 將顯示配置設置爲「無」。你可以設置一個速度爲 消失'快','正常'或'慢'和一個回調函數 在影響被允許後執行。

$("#class_div").hide();

之前 <div id='class_div'>Hello world</div> <div id='class_div_2'>Hello world</div>

後: <div id='class_div' style="display:none;">Hello world</div> <div id='class_div_2'>Hello world</div>

+1

確認!我的眼睛 - 請爲了您自己的理智 - 使用[模板引擎],或者至少一些[heredoc](http://http://www.webresourcesdepot.com/19-promising-php-template-engines/) /www.tuxradar.com/practicalphp/2/6/3)! – JonnyReeves 2012-03-08 22:49:30

+0

請顯示呈現的HTML/JS代碼... – powtac 2012-03-08 22:50:30

+0

我很抱歉!我應該怎麼做才能使代碼更具可讀性?我不習慣編碼,也不知道heredoc,也不知道如何使用模板angines。這是一段不是我的代碼,我只是想了解它。 – Rosamunda 2012-03-08 23:09:36

回答

0

.hide可能是一個jQuery功能。我認爲你從頁面中刪除了jQuery。在FireBug中,進入腳本選項卡並在加載的文件中查找jQuery。你看到了嗎?

+0

你是正確的,但$似乎被定義。這是適當的其他框架運行以及... – powtac 2012-03-08 22:51:36

+0

謝謝,是的,它被稱爲:它是那裏。這是一個Drupal的安裝,並且此代碼用於工作,直到我改變了CSS ... – Rosamunda 2012-03-08 23:11:00

+0

好吧,我認同答案是最好的,但問題沒有得到解決...:'( – Rosamunda 2012-03-12 18:38:32

1

包括jQuery在您的頁面的頭部,因爲傑夫已經指出。如果你已經加載了jQuery並且仍然存在這個問題,那麼你還將加載prototype.js/mootools/dojo或者你對$函數的修補。要在其他庫旁邊使用jQuery,請在包含之後直接調用'jQuery.noConflict()',並在腳本中將'$'替換爲'jQuery'。

+0

謝謝!但是,我沒有使用mootools,這個主題基於Twitter的Bootstrap,也使用jQuery ... – Rosamunda 2012-03-09 01:20:20

+0

這段代碼可能與文件中的其他javascript衝突嗎? – Rosamunda 2012-03-09 15:39:55

0

如果jQuery在noconflict模式下加載,不管你使用的是mootools還是任何其他庫,你都需要用jQuery替換$或者在你的函數中將$賦給jQuery。

EG

jQuery("#class_div").hide(); 
相關問題