2011-07-29 37 views
0

不知道什么是解决这个如何让每一个下拉菜单和文本区域排

我想基于什么在下拉列表中选择来控制TE textarea的原因正确的方法的ID。正如你可以看到下面我已经到了我可以得到DropDown列表的每一行的属性ID的阶段状态但我无法得到每个textarea的attr id原因

我的最终结果应该是如果一个用户选择拒绝,然后用户输入一个原因,如果停用必须输入日期。在此先感谢

<tr> 
<td class="ms-vb"><span dir="none"> 
<select class="ms-RadioText" title="Status" id="WebPartManager_g_31f0d9e1_72a0_4ef2_b286_7c30cd0fda1f_ff5_3_ctl00_DropDownChoice" name="WebPartManager$g_31f0d9e1_72a0_4ef2_b286_7c30cd0fda1f$ff5_3$ctl00$DropDownChoice"> 
<option value="Provisionally Approved" selected="selected">Provisionally Approved 
</option> 
<option value="Approved">Approved</option> 
<option value="Declined">Declined</option> 
<option value="Deactivated">Deactivated</option> 
</select><br> 
</span></td> 
<td class="ms-vb"> 
<span dir="none"> 
<textarea dir="none" class="ms-long" title="Reason" id="WebPartManager_g_31f0d9e1_72a0_4ef2_b286_7c30cd0fda1f_ff11_3_ctl00_ctl00_TextField" cols="20" rows="6" name="WebPartManager$g_31f0d9e1_72a0_4ef2_b286_7c30cd0fda1f$ff11_3$ctl00$ctl00$TextField" style="display: none;">Test3</textarea><br> 
</span></td> 

</tr> 
$(document).ready(function(){ 

$("textarea").hide(); 
$("input[title$='DeActivatedDate']").hide(); 


$("tr select[title='Status']").change(function() { 




     var selectedValue = $(this).find("option:selected").text(); 


     if (selectedValue == 'Declined') 
     { 


     alert('You need to type in a Reason if STATUS is DECLINED!'); 


     //i'm getting the id of each dropdown row here 
     var select_id = $(this).attr('id'); 
     var b = $("#" + select_id).text(); 
     alert(select_id); 
     //i can get the id of each Dropdown of each row 


     //i'm getting the id of each textarea row here 
     var textarea_id = $("tr td textarea").attr('id'); 
     var c = $("#" + textarea_id).text(); 
     alert(textarea_id); 

     //i'm only getting the attr id of the first textarea but not the other 



     } 

     if (selectedValue == 'Deactivated') 
     { 

     alert('You need to select a De-Activated Date if STATUS IS DEACTIVATED!'); 
     } 




     }); 
}); 

回答

2

attr function

得到属性值仅在第一个匹配的元素设置

所以这样的:

var select_id = $(this).attr('id'); 

作品,因为this<select>元素,所以只有一个元素。该<textarea>之一:

var textarea_id = $("tr td textarea").attr('id'); 

只是抓住第一<textarea>id属性。如果你希望所有的<textarea>元素,使用each

$("tr td textarea").each(function() { 
    var textarea_id = this.id; 
    var content  = $(this).text(); 
    alert(textarea_id); 
}); 

你并不需要使用attr如果你有一个DOM对象(thiseach内),以获得一个ID,你可以直接访问属性。

如果你只想要在同一行是作为<select>(如在评论中指出),那么你可以使用closest走了DOM了一下<textarea>然后find回来了下来:

var textarea_id = $(this).closest('tr').find('textarea').attr('id'); 

而且有表行中的多个<textarea>元素,你想他们,那么这两种方法相结合:

$(this).closest('tr').find('textarea').each(function() { 
    // ... 
}); 
+0

感谢。但我不想要所有textarea的id。我非常希望与dropdownlist相同的那一行。我需要将所选项目与textarea框中的值进行比较。如果选择的值被拒绝,则用户必须在textarea的相应行中添加一些文本。 – naijacoder

+0

@naijacoder:看我的更新。 –

+0

谢谢亩太短会给那个尝试让你知道 – naijacoder