用js寫倒計時,向列表添加數據-------2017-03-21


一、單選按鈕確定提交是否可用

<input id="a" type="radio" name="a"  onclick="check()"/>

<label for="a">同意</label>

<input id="b" type="radio" name="a"  onclick="check()" />

<label for="b">不同意</label><br />

<input type="button" name="d" id="c" value="提交" disabled="disabled"/>

<script>

function check(){

var yes=document.getElementById("a");

var no=document.getElementById("b");

var btn=document.getElementById("c");

//  if(yes.getAttribute("checked")) 這個行不通,記住下面這個yes.check

if(yes.checked)

{

btn.removeAttribute("disabled");

}

if(no.checked){

btn.setAttribute("disabled","disabled");

}

}

</script>

顯示效果如下:

未點擊前,提交按鈕不可用;在點擊同意之后后提交按鈕可用。

 

                       

二、倒計時:

Eg1:

<script>

function shijian(){

alert("哈哈");

}

window.setTimeout("shijian()",2000);

</script>

注:setTimeoout屬性是指延長多少時間發生的事,以毫秒計數。

此處顯示效果: 哈哈界面就會在刷新頁面2000毫秒后彈出。

Eg2:

<script>

function shijian(){

alert("哈哈");

}

for(i=0;i<4;i++){

window.setTimeout("shijian()",i*2000);

}

</script>

顯示效果:利用for循環控制顯示次數,即每隔2秒顯示一次哈哈,共顯示4次。

Eg3:

<span id="time">8</span>

<input id="btn" name="next" type="button" disabled="disabled" value="下一步" />

<script>

function shijian(){

var s=document.getElementById("time").innerText;

s=parseInt(s);         -------變量轉換成整數。

if(s<=0){                  ------當秒數小於0時,“下一步”按鈕生效。

document.getElementById("btn").removeAttribute("disabled");

}

else{                      

s--;                        -------時間每隔2秒減1

document.getElementById("time").innerText=s;

}

}

for(i=0;i<=9;i++){         -------設置循環次數和時間間隔

window.setTimeout("shijian()",i*2000);

}

</script>

注:次數的設置,可能會因為少一次導致按鈕不可用。

 

三、向列表內添加數據:

1、利用<ol>列表

<body>

<ol id="ol1">                         -------定義一個有序列表

<li>開始值</li>    

</ol>

 

<input type="text" id="txt"/>          -----------輸出一個文本框

<input type="button" value="添加" id="btn" onclick="add()"/>

                      -----------設置一個添加按鈕,並創建一個點擊事件     

<script>

function add()

{

var x = document.getElementById("txt").value;   ---將文本內容定義給x

var y = document.getElementById("ol1");      -------將列表內容定義給y

y.innerHTML+="<li>"+x+"</li>";           --------將x值添加到y中

}

</script>

</body>

2、利用<select>做列表

<select size="7" id="ol1">   -----顯示一個有5列的列表,最多可到7列

<option>1</option>

<option>2</option>

<option>3</option>

<option>4</option>

<option>5</option>

</select>

<input type="text" id="txt"/>

<input type="button" value="添加" id="btn" onclick="add()"/>

<script>

function add()

{

var x = document.getElementById("txt").value;

var y = document.getElementById("ol1");

y.innerHTML+="<option>"+x+"</option>";

}

</script>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Everything will go okay. Just go ahead.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM