如何获取每个下拉列表和textarea行的ID

时间:2011-07-29 06:22:30

标签: jquery sharepoint-designer

不确定解决此问题的正确方法是什么

我想根据下拉列表中选择的内容控制te textarea Reason。正如你在下面看到的,我已经到了可以获得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!');
       }




      });
});

1 个答案:

答案 0 :(得分:2)

attr function

  

仅获取匹配集

中第一个元素的属性值

所以这个:

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

有效,因为this<select>元素,因此只有一个元素。 <textarea>一个:

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

只需获取第一个id的{​​{1}}属性即可。如果您想要所有<textarea>元素,请使用each

<textarea>

如果您有$("tr td textarea").each(function() { var textarea_id = this.id; var content = $(this).text(); alert(textarea_id); }); 内的DOM对象(attr),则无需使用this来获取ID,您只需直接访问该属性即可。

如果您只想要与each位于同一行的<textarea>(如评论中所述),那么您可以使用closest向前走一段DOM然后find回来:

<select>

表格行中有多个var textarea_id = $(this).closest('tr').find('textarea').attr('id'); 个元素,你想要它们全部,然后结合两种方法:

<textarea>