简体   繁体   中英

Javascript code without errors but doesn't work

No 'real' errors pop up in the console or in JsLint. I personally prefer using onclick in HTML, but I switched to $("#buttonID1").click() as part of debugging. I have no idea what's wrong with the code. Live version here DEMO

Input: Amazon_Laser_BobtheBodyBuilder_07-11-13.xls

Expected output: |-\\n| Amazon\\n| Laser\\n| BobtheBodyBuilder\\n| July 7, 2013\\n

Actual output: Nothing at all!

JS

function emptyText() {
    $('#textinput').val('');
    $('#textoutput').val('');
}

$("#buttonID1").click(function(){
    var input = document.getElementById('textinput').value;
    var lines = input.split('\n');
    var output = '';
    $.each(lines, function(key, line) {
        var results = line.split(/[_\.]/);
            if(results!==null && results.length!== 0)
            {
                var a = '|-\n| '+results[0]+'\n';
                var b = '| '+results[1]+'\n';
                var c = '| '+results[2]+'\n';
                var d = results[3];
                var e = '| '+results[4]+'\n';

                var date = d.split(/[\-]/);
                var mm = date[0];
                var dd = date[1];
                var yy = date[2];
                var month = '';
                switch(mm){
                    case "01":
                        month='Jan';
                        break;
                    case "02":
                        month='Feb';
                        break;
                    case "03":
                        month='Mar';


            break;
                case "04":
                    month='Apr';
                    break;
                case "05":
                    month='May';
                    break;
                case "06":
                    month='June';
                    break;
                case "07":
                    month='Jul';
                    break;
                case "08":
                    month='Aug';
                    break;
                case "09":
                    month='Sep';
                    break;
                case "10":
                    month='Oct';
                    break;
                case "11":
                    month='Nov';
                    break;
                case "12":
                    month='Dec';
                    break;
                default:
                    break;
            }

            switch(yy){
                case "10":
                    yy='2010';
                    break;
                case "11":
                    yy='2011';
                    break;
                case "12":
                    yy='2012';
                    break;
                case "13":
                    yy='2013';
                    break;
                default:
                    break;
            }

            var date2 = '| '+month+' '+dd+', '+yy+'\n';
            output += a+b+c+date2+e;
        }
});
});

HTML

<!DOCTYPE html>
<html>
 <head>
  <meta charset="UTF-8">
  <title>title</title>
  <link rel=href="https://dl.dropboxusercontent.com/u/101322542/Public%20Codes/mister%20table/Part%202%20-%20tokenizer/tokenizer.css" />

  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
  <script src="https://dl.dropboxusercontent.com/u/101322542/Public%20Codes/mister%20table/Part%202%20-%20tokenizer/tokenizer.js" type="text/javascript"></script>
 </head>
 <body>
   <p class="left">Put your variables here:</p>
  <textarea rows="4" cols="40" id="textinput">
</textarea>
   <br>
     <button type="button" id="buttonID1">AutoType</button>
     <button type="button" id="buttonID2" onclick="emptyText()">Clear</button>
   <p class="right">Output:</p>
   <textarea rows="4" cols="40" id="textoutput">
</textarea>
 </body>
</html>

CSS

#textinput{
  background-color:#285c00;
  color:white;
}

#textoutput{
  background-color:#285c00;
  color:white;
}

You forgot to use the string you build. Add this at the end of your function :

$('#textoutput').val(output);

Demonstration


It's not the core problem but you could make your code a little simpler in a few places.

For example this block

switch(yy){
    case "10":
        yy='2010';
        break;
    case "11":
        yy='2011';
        break;
    case "12":
        yy='2012';
        break;
    case "13":
        yy='2013';
        break;
    default:
        break;
}

could be replaced with

yy = parseInt(yy, 10);
if (yy<100) yy+=2000;

(this makes a number, which is appropriate to handle numbers, you can make it a string again by using ''+yy after if you like).

You should change the last line to;

output += a+b+c+date2+e;
$('#textoutput').val(output);

You need to actually add text with the button. So, basically, you forgot this line:

document.getElementById('textoutput').value = output;

after this line:

output += a+b+c+date2+e;

in the JavaScript file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM