JS 以及JQuery 獲取文字框,單選按鈕的值
阿新 • • 發佈:2018-11-14
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="Generator" content="EditPlus®"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> <title>Document</title> <style></style> <script src="js/jquery-latest.js"></script> <script> function tt(){ var name=document.getElementById("Name").value; console.log(name); var sex=document.getElementsByName("sex"); var sexValue; for(var i=0;i<sex.length;i++){ if(sex[i].checked==true){ sexValue=sex[i].value; break; } } console.log(sexValue); var hobby=document.getElementById("select").value; console.log(hobby); } function tt2(){ var n1=$("#Name").val(); var n2=$("input[name='NameN']").val(); var t1=$("input[name='sex']").val(); var t2=$("input[name='sex']:checked").val(); var tt1=$("#select").val(); var tt2=$("select[name='selectN']").val();//find("option :selected").text(); var tt3=$("input[name='selectN']").val(); alert("*"+n1+"*"+n2+"*"+t1+"*"+t2+"*"+tt1+"*"+tt2+"*"+tt3); } function tt3(){ $("#div").load("txt/new.txt",function(responseTxt,statusTxt,xhr){ if(statusTxt=="success") alert("外部內容載入成功!"); if(statusTxt=="error") alert("Error: "+xhr.status+": "+xhr.statusText); }); } </script> </head> <body> 姓名: <input type="text" name="NameN" id="Name" value="1"> 性別: <input type="radio" name="sex" value="female" checked>女 <input type="radio" name="sex" value="male">男 愛好: <select name="selectN" id="select"> <option value="ball" selected>打球</option> <option value="read">看書</option> </select> <input type="button" value="tt" onclick="tt()"> <input type="button" value="tt2" id="btn" onclick="tt2()"> <input type="button" value="tt3" id="btn1" onclick="tt3()"> <div id="div" style="width:100px;height:100px;border:red solid 1px;"></div> </body> </html>