2014年11月13日 星期四

ASP.NET WebForm + JQuery RadioButtonList變更時,啟用DropDownList控制項

本篇直接使用JQuery來啟用或停用DropDownList,重點不使用ASP.NET的AJAX功能

前置作業:請先到JQuery官方網站下載jquery(我是使用v1.8.2)

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="./Scripts/jquery.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            //小於%=RBL_bhours.UniqueID %大於 是ASP的寫法,會將RBL_bhours控制項的唯一識別印出(類似ID)
            $('input[type="radio"][name="<%=RBL_bhours.UniqueID %>"]').change(function () {
                var bhours = this.value;
                //會先找到id="div1" 再 循序處理每個 select元素
                $('#div1 select').each(function () {
                    if (bhours == 'true') {
                        $(this).removeAttr('disabled'); //移除disabled等於啟用select元素
                    } else {
                        $(this).attr('disabled', 'disabled'); //加入disabled等於停用select元素
                    }
                });
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:RadioButtonList ID="RBL_bhours" runat="server"
            RepeatDirection="Horizontal" RepeatLayout="Flow">
            <asp:ListItem Value="true">開啟</asp:ListItem>
            <asp:ListItem Value="false" Selected="True">關閉</asp:ListItem>
        </asp:RadioButtonList>
        &nbsp;&nbsp;
        <div id="div1">
            每月
            <asp:DropDownList ID="DDL_ts" runat="server" Width="45px" Enabled="false">
                <asp:ListItem>01</asp:ListItem>
                <asp:ListItem>02</asp:ListItem>
                <asp:ListItem>03</asp:ListItem>
            </asp:DropDownList>日
            到
            每月
            <asp:DropDownList ID="DDL_te" runat="server" Width="45px" Enabled="false">
                <asp:ListItem>01</asp:ListItem>
                <asp:ListItem>02</asp:ListItem>
                <asp:ListItem>03</asp:ListItem>
            </asp:DropDownList>日
        </div>
    </form>
</body>
</html>

2014年11月12日 星期三

javascript 應用篇




2017/06/16
取代字串中所有的blue為red。
var str = "Mr Blue has a blue house and a blue car".replace(/blue/g, "red");
取代字串中所有的blue為red,不區分大小寫。
var str = "Mr Blue has a blue house and a blue car".replace(/blue/gi, "red");
(參考:JavaScript String replace() Method)

2017/06/13
字串轉數值時,須注意字串前面是不是多了個0,當字串的數字是08、09時,parseInt會把它當作八進制處理,但是08、09都是不正確的八進制,我們可以在parseInt再加上第二個參數,例如:parseInt("08",10),告訴parseInt按照十進制處理,這樣就沒有問題了。
(參考:關于javascript中parseInt函數的一個所謂的bug)

2015/07/17
1. 全選checkbox。
<Script type="text/javascript">
function stcb(objcb){
  var checkboxs = document.getElementsByName('CB_Del'); 
  for(var x=0;x<checkboxs.length;x++){
    checkboxs[x].checked = objcb.checked;
  };
};
</Script>
<input type="checkbox" name="cb_total" id="cb1" onclick="stcb(this);" />
<label for="cb1" style=" font-size:10px;">全選</label>
<input type="checkbox" name="CB_Del" value="1">
<input type="checkbox" name="CB_Del" value="2">
<input type="checkbox" name="CB_Del" value="3">
<input type="checkbox" name="CB_Del" value="4">
<input type="checkbox" name="CB_Del" value="5">

2.變更form的action,並送出。
<Script type="text/javascript">
function but1_click(){
  document.f1.action='test.asp';
  document.f1.submit();
};
</Script>
....
<form name="f1" method="post">
<input name="but1" type="button" value="按鈕" onclick="but1_click();">
</form>
....

2015/05/20
1.變更link標籤的css檔
Step 1.首先再head標籤中引入css,id為cf1,如下:
<head>
...
<link rel="stylesheet" href="" id="cf1" />
...
</head>
Step 2.寫入下方的JavaScript,並呼叫它,即可修改CSS。
function change_css(){
  var css = document.getElementById("cf1");
  css.href = 'stylesheets/css1.css';
};

2.兩個日期時間相減,經過多少分鐘(取得經過多少毫秒轉分鐘)。
var d1=new Date("2015/05/19 19:18"); //假設登入時間
//getTime() 取得時間 (由 1970-01-01 00:00 到目前時間) 單位:(毫秒)
document.write('d1='+d1+' d1_num='+d1.getTime()+'<br/>');
var d2=new Date("2015/05/20 19:18"); //假設現在時間
document.write('d2='+d2+' d2_num='+d2.getTime()+'<br/>');
var num=d2.getTime()-d1.getTime(); //經過多少毫秒=目前時間(毫秒)-登入時間(毫秒)
document.write('num='+num+'<br/>');
//若要將毫秒數轉換為天數,請用 86,400,000 (1000 毫秒 x 60 秒 x 60 分鐘 x 24 小時) 除以毫秒數
document.write('毫秒 轉 分鐘='+(num/(1000*60))+'<br/>');

2015/03/30
1.按下按鈕出現請稍候訊息(請自行將ButtonFinish方法寫在Button的onclick事件中)
    <script type="text/javascript">
        var strpoint = '';
        function rec() {
            if (strpoint != '') {
                if (strpoint.length <= 2) {
                    strpoint = strpoint + '.';
                } else {
                    strpoint = '';
                };
            } else {
                strpoint = '.';
            };
            $('#span1').html('訊息:請稍候' + strpoint);
            setTimeout("rec();", 1000);
        };
        function ButtonFinish() {
            setTimeout(function () { rec(); }, 500);
        };
    </script>

2014/11/13
1.判斷手機是否為瀏覽器或手機瀏覽
    function checkerAgent() {
        var flag = false;
        if (navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry|IEMobile)/)) {
            flag = true;
        } else {
            //this is the browser
            flag = false;
        };
        if (flag == true) {
            document.location.href = './index1.html';
        } else {
            document.location.href = './index2.html';
        };
    };

2.倒數5秒鐘
    var s1=5;
    function rec() {
        if (s1 != 0) {
            $('#span1').text('倒數' + s1 + '秒'); //使用jquery設定span標籤
            setTimeout("rec();", 1000);
            s1 = s1 - 1;
        } else {
             $('#span1').text('時間到!!');
        };
    };

2014年11月10日 星期一

javascript 驗證表單格式


利用JQuery 取得欄位值,在透過javascript驗證檢查資料格式是否正確
function check_form(conphone, email) {
var str_message='';
var reg, strtel, strEmail;
strtel = ($('#' + conphone).val()).trim(); strEmail = ($('#' + email).val()).trim();
reg =/^[A-Z]\d{9}$/;  //身份證字號
reg = /(09[0-9]{2}-[0-9]{3}-[0-9]{3})|([0-9]{2}-[0-9]{6,8})$/;
if (reg.test(strtel) == false) {
str_message = f1(str_message, '聯絡電話格式錯誤,範例:0912-111-111或06-7211111');
};
reg = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z]+$/;
if (reg.test(strEmail) == false) {
str_message = f1(str_message, 'E-mail格式錯誤,範例:test@gmail.com');
};

if (str_message != '') {
alert(str_message);
return false;
};
return confirm("確定要送出嗎?");
};

function f1(str_mess, str_add) {
    if (str_mess == '') { str_mess = str_add + '\n'; } else { str_mess = str_mess + str_add + '\n'; };
    return str_mess;
};

2014年11月6日 星期四

ASP(Active Server Pages) 應用技巧篇


2014/11/07
1.取得host,並組成網址
<%
        '取得host,並檢查是否是https
if Request.ServerVariables("HTTPS")="on" then
str_url="https://" & Request.ServerVariables("HTTP_HOST")
else 'off
str_url="http://" & Request.ServerVariables("HTTP_HOST")
end if
Response.write(str_url)

        '========================
        '印出Request.ServerVariables所有資料
'for each x in Request.ServerVariables
'    response.write(x & "=" & Request.ServerVariables(x) & "<br>")
'next
%>