2009-12-18 48 views
0

我正在使用Zend框架來顯示html表單上的radio選項列表。我必須使用無線電元素 ,因爲我希望用戶看到頁面上的所有選項,否則我會使用複選框。 我正在填充數據庫查詢中的無線電列表。Zend Radio元素和jQuery選擇器獲取ID和值

$sectorname = new Zend_Form_Element_Radio('sectorname'); 
$sectorname->setLabel('Sector'); 
$sectorname->setSeparator(' '); 
$sectorTable = new Model_DbTable_Sector(); 
$sectors = $sectorTable->listSectors(); 
foreach($sectors as $sector)  
{ 
    $sectorname->addMultiOption($sector->id,$sector->name); 
} 
$sectorname->setDecorators($decors); 
$this->addElement($sectorname); 

$sectorid = new Zend_Form_Element_Hidden('sectorid'); 
$sectorid->setDecorators($decors); 
$this->addElement($sectorid); 

這是創建這個樣子的HTML中,你可以看到,該部門的ID「和「名稱」上述被映射到HTML的標籤值和輸入「身份證」。還有一個隱藏的扇區html元素。

<tr> 
<th>Sector</th> 
<td> 
<label for="sectorname-1"><input name="sectorname" id="sectorname-1" value="1" type="radio">Accountants</label> 
<label for="sectorname-36"><input name="sectorname" id="sectorname-36" value="36" type="radio">Admin</label> 
<label for="sectorname-2"><input name="sectorname" id="sectorname-2" value="2" type="radio">Agriculture</label> 
<label for="sectorname-5"><input name="sectorname" id="sectorname-5" value="5" type="radio">Architects</label> 
<label for="sectorname-11"><input name="sectorname" id="sectorname-11" value="11" type="radio">Army</label> 
<label for="sectorname-48"><input name="sectorname" id="sectorname-48" value="48" type="radio">Undefined</label></td> 
<input name="sectorid" value="" id="sectorid" type="hidden">    
</tr> 

此刻,我使用JQuery捕獲標籤/無線電元素上的click事件,並更新隱藏的sectorid值。

->addOnLoad('$("input[name=\'sectorname\']").click( 
    function() 
    { 
     $("#sectorid").val(this.val); 
    });'); 

我的問題是'sectorname'和'sectorid'元素都是數字值。有沒有與jQuery的方式,我可以獲得標籤元素的字符串值,並將其設置到我的窗體中隱藏的元素?

+0

可能 - 但要獲得一個接受答案是正確的 – emeraldjava 2009-12-18 12:52:14

回答

0

試試這個

$("#sectorid").val($(this).parent().text()); 
+0

都能跟得上 - 輸入元素沒有任何文本值。該文本屬於父標籤元素。 – emeraldjava 2009-12-18 12:51:35

+0

$(this).parent()。text() – emeraldjava 2009-12-18 12:53:12