公输逍遥 公输逍遥
关注数: 9 粉丝数: 148 发帖数: 2,097 关注贴吧数: 28
跪求各位大佬伸出友情的援手 跟着书上做了一个表单的例子,目前想要实现的效果是如果没有选择单选按钮,该单选按钮的标签和按钮本身就转变为红色的粗体。然而我照着书上的代码敲了一遍,运行下来发现,为选择单选按钮点击提交,什么事情都没有发生,同时所有的项目都填写完毕以后,点击提交也毫无反应。研究了一天了,没个结果,无奈之下只好来求助各位大佬了。 js代码如下: window.onload = initForms; function initForms() { for(var i = 0;i < document.forms.length;i++) { document.forms[i].onsubmit = validForm; } } function validForm() { var allGood = true; var allTags = document.getElementsByTagName("*"); for(var i = 0;i < allTags.length;i++) { if(!validTag(allTags[i])) { allGood = false; } } return allGood; function validTag(thisTag) { var outClass = ""; var allClasses = thisTag.className.split(" "); for(var j = 0;j < allClasses.length;j++) { outClass += validBasedOnClass(allClasses[j]) + " "; } thisTag.className = outClass; if(outClass.indexOf("invalid") > -1) { invalidLabel(thisTag.parentNode); thisTag.focus(); if(thisTag.nodeName == "INPUT") { thisTag.select(); } return false; } return true; function validBasedOnClass(thisClass) { var classBack = ""; switch(thisClass) { case "": case "invalid": break; case "reqd": if(allGood && thisTag.value == "") { classBack = "invalid "; } classBack += thisClass; break; case "radio": if(allGood && !radioPicked(thisTag.name)) { classBack = "invalid "; } classBack += thisClass; break; case "isNum": case "isZip": case "email": classBack += thisClass; break; default: if(allGood && !crossCheck(thisTag, thisClass)) { classBack = "invalid "; } classBack += thisClass; } return classBack; } function crossCheck(inTag, otherFieldID) { if(!document.getElementById(otherFieldID)) { return false; } return(inTag.value != "" || document.getElementById(otherFieldID).value != ""); } function radioPicked(radioName) { var radioSet = ""; for(var k = 0;k < document.forms.length;k++) { if(!radioSet) { radioSet = document.forms[k][radioName]; } } if(!radioSet) { return false; } for(k = 0;k < radioSet.length;k++) { if(radioSet[k].checked) { return true; } } return false; } function invalidLabel(parentTag) { if(parentTag.nodeName == "LABEL") { parentTag.className += " invalid"; } } } } html代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset= utf-8"> <title>Car Picker</title> <link rel = "stylesheet" href = "css/script06.css"> </head> <body> <h2 class = "centered">Car Picker</h2> <form action = "someAction.cgi"> <p><label for = "emailAddr">Email Address; <input id = "emailAddr" type = "text" size = "30" class = "reqd email"> </label></p> <p><label for = "color">Colors: <select id = "color" class = "reqd"> <option value = "" selected>Choose a color</option> <option value = "Red">Red</option> <option value = "Green">Green</option> <option value = "Blue">Blue</option> </select> </label></p> <p>Options: <label for = "sunroof"><input type = "checkbox" id = "sunroof" value = "Yes">Sunroof(Two door only)</label> <label for = "pWindows"><input type = "checkbox" id = "pWindows" value = "Yes">Power Windows</label> </p> <p><label for = "DoorCt">Doors:&nbsp;&nbsp; <input type = "radio" id = "twoDoor" name = "DoorCt" value = "twoDoor" class = "radio">Two <input type = "radio" id = "fourDoor" name = "DoorCt" value = "fourDoor" class = "radio">Four </label></p> <p><label for = "zip">Enter your Zip code or pick the dealer nearest you:<br> Zip:<input id = "zip" type = "text" size = "5" maxlength = "5" class = "isZip dealerList"> <select id = "dealerList" size = "4" class = "zip"> <option value = "California--Lemon Grove">California--Lemon Grove</option> <option value = "California--Lomita">California--Lomita</option> <option value = "California--Long Beach">California--Long Beach</option> <option value = "California--Los Alamitos">California--Los Alamitos</option> <option value = "California--Los Angeles">California--Los Angeles</option> </select> </label></p> <p><input type = "submit" value = "Submit">&nbsp;<input type = "reset"></p> </form> <script src = "js/script04.js"></script> </body> </html> css样式表如下: body { color: #000; background-color: #FFF; } input.invalid { background-color: #FF9; border: 2px red inset; } label.invalid { color: #F00; font-weight: bold; } select { margin-left: 80px; } input { margin-left: 30px; } input+select, input+input { margin-left: 20px; } .centered { text-align: center; }
大佬们我又来了,求助一个小程序 代码如下: import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; import java.text.*; public class TimeForm extends HttpServlet { private static final String CONTENT_TYPE = "text/html"; private Locale[] allLocale = Locale.getAvailableLocales(); private String[] allTimeZone = TimeZone.getAvailableIDs(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(CONTENT_TYPE); PrintWriter out = response.getWriter(); out.println("<h3>Choose locale and time zone</h3>"); out.println("<form method=\"post\" action=" + "/web.xml/TimeForm>"); out.println("Locale <select size=\"1\" name=\"locale\">"); for(int i = 0;i < allLocale.length;i++) { out.println("<option value=\"" + i + "\">" + allLocale[i].getDisplayName() + "</option>"); } out.println("</select>"); out.println("<p>Time Zone<select size=\"1\" name=\"timezone\">"); for(int i = 0;i < allTimeZone.length;i++) { out.println("<option value=\"" + allTimeZone[i] + "\">" + allTimeZone[i] + "</option>"); } out.println("</select>"); out.println("<p><input type=\"submit\" value=\"Submit\" >"); out.println("<input type=\"reset\" value=\"Reset\"></p>"); out.println("</form>"); out.close(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(CONTENT_TYPE); PrintWriter out = response.getWriter(); response.setCharacterEncoding("GB18030"); out.println("<html>"); int localeIndex = Integer.parseInt(request.getParameter("locale")); String timeZoneID = request.getParameter("timezone"); out.println("<head><title>Current Time</title></head>"); out.println("<body>"); Calendar calendar = new GregorianCalendar(allLocale[localeIndex]); TimeZone timeZone = TimeZone.getTimeZone(timeZoneID); DateFormat dateFormat = DateFormat.getDateTimeInstance( DateFormat.FULL, DateFormat.FULL, allLocale[localeIndex]); dateFormat.setTimeZone(timeZone); out.println("Current time is " + dateFormat.format(calendar.getTime()) + "</p>"); out.println("</body></html>"); out.close(); } } 最后显示的时候出现了一个问题,就是locale栏中全是问号乱码,我自己也黑人问号了,如下图所示:求各位大佬帮帮忙
1 下一页