如何透過jQuery去存取asp.net的RadioButtonList跟RadioButton_兩情境

 



在asp.net webform中控件RadioButtonList是由很多ListItem組成
可自行安排要水平或垂直(預設)佈局

當RadioButtonList遇上jQuery
由於工作專案中遇到要透過jquery判別如選取了不受理
就在旁邊顯示(必填紅星星)





html部分

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<tr>
    <td align="right" class="width15">
        <div class="font-title titlebackicon"></span>受理狀況</div>
    </td>
    <td class="" colspan="3" align="right" nowrap="nowrap">
        <asp:RadioButtonList ID="rbl_reception_status" runat="server" RepeatDirection="Horizontal" CssClass="width100" AutoPostBack="True">
            <asp:ListItem Value="1" Selected="True">未處理</asp:ListItem>
            <asp:ListItem Value="2">通知取件</asp:ListItem>
            <asp:ListItem Value="3">已完成流通</asp:ListItem>
            <asp:ListItem Value="5">取消申請</asp:ListItem>
            <asp:ListItem Value="4">不受理</asp:ListItem>
        </asp:RadioButtonList>
    </td>
</tr>
<tr>
    <td align="right" class="width15">
        <%--<div class="font-title titlebackicon"><span class="font-red">*</span> 申請理由</div>--%>
        <div id="dv_fail_reason" class="font-title titlebackicon">不受理原因</div>
    </td>
    <td class="" colspan="3" align="right" nowrap="nowrap">
        <asp:TextBox ID="txt_fail_reason" runat="server" CssClass="width100"></asp:TextBox>
    </td>
</tr>

js/jquery部分


1
2
3
4
5
6
7
8
function AddPromptRdoBtnListRcptStatus() {
            var rbl_val = $('#<%=rbl_reception_status.ClientID%>').find('input:checked').val();
            if (rbl_val != "" && rbl_val != undefined) {
                if (rbl_val == 4) {
                    $('#dv_fail_reason').prepend('<span class="font-red">*</span> ');//在被選元素內的開頭插入內容(在元素內)
                }
            }
        }

這裡使用jQuery的selector對上 html element id 抓到radiobuttonlist 元素後
在透過find來抓到於RadioButtonList區塊內勾選的Listitem元素
在該Tag中我們透過value判定選定數字
相應對文字內容的div  tag中去prepend必填紅星星

註1.不是用filter而用find,find是在該元素Tag內(下)在尋找,filter則是在整個form下該元素類別的整個去過濾篩選。一個是對其內部子集合(三年A班的該班級),一個是對自身集合元素做篩選(三年級從A~Z班)

註2.這裡用prepend而非append、after、before
append:在被選元素內的結尾插入內容(在元素內)
prepend:在被選元素內的開頭插入內容(在元素內)
after:在被選元素之後插入內容(在元素外)
before:在被選元素之前插入內容(在元素外)

我們預期目標:
Before
<div id="dv_fail_reason" class="font-title titlebackicon">不受理原因</div>
After
<div class="font-title titlebackicon"><span class="font-red">*</span> 不受理原因</div>

效果測試結果:

append:在被選元素內的結尾插入內容(在元素內)
$('#dv_fail_reason').append('<span class="font-red">*</span> ');

After
<div id="dv_fail_reason" class="font-title titlebackicon">不受理原因<span class="font-red">*</span> </div>


prepend:在被選元素內的開頭插入內容(在元素內)
$('#dv_fail_reason').prepend('<span class="font-red">*</span> ');

After
<div id="dv_fail_reason" class="font-title titlebackicon"><span class="font-red">*</span> 不受理原因</div>



after:在被選元素之後插入內容(在元素外)
$('#dv_fail_reason').after('<span class="font-red">*</span> ');
After
<div id="dv_fail_reason" class="font-title titlebackicon">不受理原因</div><span class="font-red">*</span>


before:在被選元素之前插入內容(在元素外)
$('#dv_fail_reason').before('<span class="font-red">*</span> ');
After
<span class="font-red">*</span> <div id="dv_fail_reason" class="font-title titlebackicon">不受理原因</div>


當RadioButton遇上jQuery


再來的是我們歸還區塊輸入的欄位也是有動態提示



html部分

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<tr>
    <td colspan="3"><span class="font-size3 font-bold">歸還狀況</span></td>
    <td align="right">
        <asp:Button ID="btn_update_return_status" CssClass="genbtnS" runat="server" Text="更新歸還狀況" OnClientClick="return ValidReturnRegionInput();" OnClick="btn_update_return_status_Click" />
    </td>
</tr>
<tr>
    <td align="right" class="width15">
        <div class="font-title titlebackicon return_field">
            <asp:RadioButton ID="rbl_return_status" Text="歸還狀況" Checked="true" GroupName="ReturnGroup" runat="server" AutoPostBack="True" />
        </div>
    </td>
    <td class="width35">
        <asp:DropDownList ID="ddl_return_status" CssClass="inputex" runat="server"></asp:DropDownList>
    </td>
    <td align="right" class="width15">
        <div class="font-title titlebackicon return_field">歸還日期</div>
    </td>
    <td class="width35">
        <asp:TextBox ID="txt_return_date" CssClass="inputex Jdatepicker" runat="server"></asp:TextBox>
    </td>
</tr>
<tr>
    <td align="right" class="width15">
        <div class="font-title titlebackicon cutknot_field">
            <asp:RadioButton ID="rbl_CutKnot_date" Text="切結日期" GroupName="ReturnGroup" runat="server" AutoPostBack="True" />
        </div>
    </td>
    <td class="width35">
        <asp:TextBox ID="txt_cutknot_date" CssClass="inputex Jdatepicker" runat="server"></asp:TextBox>
    </td>
    <td align="right" class="width15">
        <div class="font-title titlebackicon cutknot_field">切結書編號</div>
    </td>
    <td class="width35">
        <asp:TextBox ID="txt_cutknot_no" CssClass="inputex" runat="server"></asp:TextBox>
    </td>
</tr>

js/jquery部分

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
function AddPromptReturnGroup() {
            var RdoBtnChecked = $("input[name$=ReturnGroup]:checked");
            var rdo_val = RdoBtnChecked.val();
            switch (rdo_val) {
                case '<%=rbl_return_status.ID%>':
                    $(".return_field").prepend('<span class="font-red">*</span> ');//在被選元素內的開頭插入內容(在元素內)
                    break;
                case '<%= rbl_CutKnot_date.ID%>':
                    $(".cutknot_field").prepend('<span class="font-red">*</span> ');//在被選元素內的開頭插入內容(在元素內)
                    break;
                case '<%=rbl_collection_date.ID%>':
                    $(".collect_field").prepend('<span class="font-red">*</span> ');//在被選元素內的開頭插入內容(在元素內)
                    break;
            }
        }


這裡我們改換成透過
jquery 的 input元素的name來過濾GroupName且要選定的元素其值
由於radiobutton被render到client端網頁時groupname會被放置於Tag中name屬性中
且前部分可能會多串一些自動生成的內容只有尾部才跟在開發時設置的GroupName有匹配
這裡是透過語法
屬性過濾器[attribute$=val] :尾部匹配
此外radiobutton被render到client端網頁時value會和ServerControl 的 ID一致
因此藉此來做分支判斷在做跟上述一樣的jQuery操作即可

註3.屬性過濾器(常用的)
[attribute=val] : 完全匹配
[attribute!=val] :不匹配
[attribute*=val] :模糊匹配
[attribute^=val] :開頭匹配
[attribute$=val] :尾部匹配


以上是本次的開發日常技術分享







留言

這個網誌中的熱門文章

經得起原始碼資安弱點掃描的程式設計習慣培養(五)_Missing HSTS Header

經得起原始碼資安弱點掃描的程式設計習慣培養(三)_7.Cross Site Scripting(XSS)_Stored XSS_Reflected XSS All Clients

(2021年度)駕訓學科筆試準備題庫歸納分析_法規是非題