卡在第二个if语句javascript中

时间:2016-06-05 12:38:45

标签: javascript html if-statement

任何人都可以知道我的代码有什么问题!我想在第三个if语句中进行密码验证。但似乎我只是坚持我的第二个If声明!我已经上了好几个小时..不知道出了什么问题!帮助PLS!并顺便使用onkeyup事件。

package com.example.java;

import org.jxmapviewer.JXMapViewer;
import org.jxmapviewer.OSMTileFactoryInfo;
import org.jxmapviewer.input.CenterMapListener;
import org.jxmapviewer.input.PanKeyListener;
import org.jxmapviewer.input.PanMouseInputListener;
import org.jxmapviewer.input.ZoomMouseWheelListenerCenter;
import org.jxmapviewer.painter.CompoundPainter;
import org.jxmapviewer.viewer.*;
import sample7_swingwaypoints.SwingWaypoint;

import javax.swing.*;
import javax.swing.event.MouseInputListener;
import java.util.*;

/**
 * Created by Suleman on 6/5/2016.
 */
public class MapPanel {

    public static void main(String[] args) {
        JXMapViewer mapViewer = new JXMapViewer();

        // Create a TileFactoryInfo for OpenStreetMap
        TileFactoryInfo info = new OSMTileFactoryInfo();
        DefaultTileFactory tileFactory = new DefaultTileFactory(info);
        mapViewer.setTileFactory(tileFactory);

        // Use 8 threads in parallel to load the tiles
        tileFactory.setThreadPoolSize(8);

        //Initializing first and last position (program to get the coordinate from mouse click here)
        GeoPosition firstPoint = new GeoPosition(50.834722, 12.921389);
        GeoPosition lastPoint = new GeoPosition(50.839167, 12.9275);

        // Create a track from the geo-positions
        List<GeoPosition> track = Arrays.asList(firstPoint, lastPoint);
        RoutePainter routePainter = new RoutePainter(track);

        // Set the Default Location
        GeoPosition chemnitz = new GeoPosition(50.833333, 12.916667);

        //Set the focus
        mapViewer.setZoom(7);
        mapViewer.setAddressLocation(chemnitz);

        // Add interactions
        MouseInputListener mia = new PanMouseInputListener(mapViewer);
        mapViewer.addMouseListener(mia);
        mapViewer.addMouseMotionListener(mia);
        mapViewer.addMouseListener(new CenterMapListener(mapViewer));
        mapViewer.addMouseWheelListener(new ZoomMouseWheelListenerCenter(mapViewer));
        mapViewer.addKeyListener(new PanKeyListener(mapViewer));

        // Create waypoints from the geo-positions
        Set<SwingWaypoint> waypoints = new HashSet<SwingWaypoint>(Arrays.asList(
            new SwingWaypoint("Zentrum", firstPoint),
            new SwingWaypoint("TU", lastPoint)));

        // Create a waypoint painter that takes all the waypoints
        WaypointPainter<Waypoint> waypointPainter = new WaypointPainter<Waypoint>();
        waypointPainter.setWaypoints(waypoints);

        // Create a compound painter that uses both the route-painter and the waypoint-painter
        List<org.jxmapviewer.painter.Painter<JXMapViewer>> painters = new ArrayList<org.jxmapviewer.painter.Painter<JXMapViewer>>();
        painters.add(routePainter);
        painters.add(waypointPainter);

        CompoundPainter<JXMapViewer> painter = new CompoundPainter<JXMapViewer>(painters);
        mapViewer.setOverlayPainter(painter);

        // Display the viewer in a JFrame
        JFrame frame = new JFrame("FRAMEWORK");
        frame.getContentPane().add(mapViewer);
        frame.setSize(800, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

1 个答案:

答案 0 :(得分:1)

以下代码示例完全按照您所解释的方式执行。将其复制并粘贴到一个全新的HTML文件上,然后测试它以查看您获得的内容。顺便说一下;无论如何,它与你自己的代码没那么不同......

    <script>
        function validateTextbox(){
            var user        = document.getElementById("username");
            var pass        = document.getElementById('password');
            var cpass       = document.getElementById('cpassword');
            var message     = document.getElementById('confirmMessage');
            var goodColor   = "#66cc66";
            var badColor    = "#ff6666";

            //(first if statement) if its false its gonna output
            //Username must be 6 characters  if true its gonna
            //proceed to 2nd statement
            if(user.value.length >= 6){
                //(2nd if statement) if its false its gonna output
                //password must be 8 characters  if true its gonna
                //proceed to 3rd statement

                if(pass.value.length >= 8){
                    //And it does not validate password matched
                    //I dont know what went wrong
                    if(pass.value == cpass.value){
                        cpass.style.backgroundColor = badColor;
                        message.style.color         = goodColor;
                        message.innerHTML           = "Passwords match"
                    }else{
                        cpass.style.backgroundColor = goodColor;
                        message.style.color         = badColor;
                        message.innerHTML           = "Passwords does not match!"
                    }
                }else{
                    pass.style.backgroundColor      = badColor;
                    message.style.color             = badColor;
                    message.innerHTML               = "Password must be 8 characters"
                }
            }else{
                user.style.backgroundColor          = badColor;
                message.style.color                 = badColor;
                message.innerHTML                   = "Username must be 6 characters"
            }
            return false;
        }
    </script>


    <form method="post" onsubmit="return validateTextbox();">
        <div id="confirmMessage"></div>
        <input type="text"      id="username"   name="username" /> <br />
        <input type="password"  id="password"   name="password" /> <br />
        <input type="password"  id="cpassword"  name="cpassword" /> <br />
        <input type="submit" value="Register" />
    </form>

测试HERE