表单在提交后未定义(javascript)

时间:2017-12-02 16:15:22

标签: javascript html forms

请帮我解决一下这个。用户成功登录后,我想在cookie中存储用户名。但点击登录按钮后,表单未定义,我无法使用let gulp = require('gulp'); let uglify = require('gulp-uglify'); let browserify = require('browserify'); let babelify = require('babelify'); let source = require('vinyl-source-stream'); let buffer = require('vinyl-buffer'); let gutil = require('gulp-util'); let sourcemaps = require('gulp-sourcemaps'); let assign = require('lodash.assign'); let watchify = require('watchify'); let customOpts = { entries : ['./App/app.js'], extensive : ['.js'], debug: true }; let opts = assign({}, watchify.args, customOpts); let b = watchify(browserify(opts)); gulp.task('js', bundle); // so you can run `gulp js` to build the file b.on('update', bundle); // on any dep update, runs the bundler b.on('log', gutil.log); // output build logs to terminal function bundle() { return b.transform(babelify,babelify.configure()) .bundle() // log errors if they happen .on('error', gutil.log.bind(gutil, 'Browserify Error')) .pipe(source('app.js')) // optional, remove if you don't need to buffer file contents .pipe(buffer()) .pipe(uglify()) .pipe(gulp.dest('dest')); } 属性。结果,不会创建cookie。 Chrome浏览器在D:\weather-app>gulp js [00:03:47] Using gulpfile D:\weather-app\gulpfile.js [00:03:47] Starting 'js'... [00:03:57] 2112196 bytes written (10.56 seconds) [00:04:27] Finished 'js' after 41 s 条件中给出了一个错误,addEventListener if未定义(在loginForm函数原型中)。

的index.htm

createEventListener()

scripts.js中

<form action="results.htm">
    <fieldset id="deliveryinfo">
        <legend>Site Login</legend>
        <label for="usernameinput">Username</label>
        <input type="text" id="usernameinput" name="username" />
        <label for="passwordinput">Password</label>
        <input type="password" id="passwordinput" name="password" />
    </fieldset>
    <fieldset id="submitbutton">
        <input type="submit" id="submitBtn" value="Login" />
    </fieldset>
</form>

1 个答案:

答案 0 :(得分:0)

您需要设置过期日期。如果你没有设置expires cookie是会话绑定&amp;当你离开页面(提交)它被销毁。添加“path = /”也可以在所有网站页面上使用cookie。

function processCookie() {
    //cookie lifetime in milliseconds (24 hours)
    var date = new Date(new Date().getTime() + 24 * 3600 * 1000); 
    document.cookie = "username=" +document.getElementById('usernameinput').value+"; path=/; expires=" + date.toUTCString();
}