动画形式,如何在页面刷新时检查输入值?

时间:2015-09-29 13:26:32

标签: javascript jquery html css

我有一个使用动态样式的表单。考虑一下这个html

    <div class="field-name field-form-item">
        <label class="placeholder" for="name">Name</label>
        <input class="form-input" id="name" type="text" name="name" maxlength="50" size="30">
    </div>

标签是输入,带有CSS。单击标签时:

$('.placeholder').on('click focus', function() {
    $(this).addClass('ph-activated');
    $(this).siblings('input').focus();
})

然后标签动画并让用户输入输入。

如果用户不想写任何内容,动画会返回,并隐藏输入字段:

$('input').on(' blur', function(){
    if ($(this).val().length === 0) {
        $(this).siblings('label').removeClass('ph-activated');
    }
});

没关系。

但是当用户填写输入时,那么刷新页面并且其浏览器没有重置输入字段(即firefox):标签位于输入上方,即使后者不为空。

我试过了:

$(document).ready(function() {
    if ($('input').val().length) {
        $(this).siblings('label').addClass('ph-activated');
    }
})

但它似乎没有触发,我尝试了几种方法来编写这个函数。到目前为止,我从未设法将类ph-activated提供给页面刷新时填充输入的标签。

抱歉,我无法理解这一点。我只是有太多的html / css / js / php来复制粘贴

2 个答案:

答案 0 :(得分:1)

您在element中定位错误$(document).ready,因为您提到labelthis认为$(this)input,而它是document input。因此,请尝试应用以下代码,我希望页面中会有多个$.each元素,因此我使用inputs并循环遍历所有$(document).ready(function() { $('input').each(function(){ //loop through each inputs if ($(this).val().length) { $(this).siblings('label').addClass('ph-activated'); } }); })

label

DEMO - 检查ph-activated,您会发现label已添加到var app = require('express')(); var http = require('http').createServer(app); var socketServer = require('socket.io').listen(http); var fs = require('fs'); var csv_stream = fs.createWriteStream("tempLog.csv"); var time_handler = require('moment'); SerialPort = require("serialport").SerialPort; var serialPort; var portName = '/dev/ttyACM0'; //change this to your Arduino port var sendData = ""; var debug = false; app.get('/', function(req, res){ res.sendFile(__dirname + '/public/interface.html') }); socketServer.on('connection', function(socket){ console.log('a user connected'); socket.on('run', function(data){ serialPort.write('Z' + data + 'A'); console.log('write to ard'); console.log('Z' + data + 'A'); }); socket.on('stop', function(data){ serialPort.write('Y' + data + 'B'); console.log('write to ard'); console.log('Y' + data + 'B'); }); socket.on('configure', function(data){ serialPort.write('X' + data + 'C'); console.log('write to ard'); console.log('X' + data + 'C'); }); socket.on('temperatureValues', function(data){ serialPort.write(data); console.log('write data to ard'); console.log(data); }); socket.on('timeValues', function(data){ serialPort.write(data); console.log('write data to ard'); console.log(data); }); }); serialListener(debug); function SocketIO_serialemit(sendData){ //console.log(sendData.length); if(sendData.length >= 14) { var s_data = sendData.split(","); socketServer.emit('temp1',{'temp': s_data[0]}); socketServer.emit('temp2',{'temp': s_data[1]}); socketServer.emit('temp3',{'temp': s_data[2]}); csv_stream.write(time_handler().format('HH:mm:ss') + ','); csv_stream.write(s_data[0] + ',' + s_data[1] + ',' + s_data[2]); csv_stream.write('\n'); //csv_stream.end(); } else{ console.log("Error"); //var s_data = sendData.split(","); //console.log(s_data[0]); //console.log(s_data[1]); //console.log(s_data[2]); } } // Listen to serial port function serialListener(debug) { var receivedData = ""; serialPort = new SerialPort(portName, { baudrate: 9600, // defaults for Arduino serial communication dataBits: 8, parity: 'none', stopBits: 1, flowControl: false }); serialPort.on("open", function () { console.log('open serial communication'); // Listens to incoming data serialPort.on('data', function(data) { if (data.toString() == "X") { console.log("Control Done"); socketServer.emit('Completed',{'key': data.toString()}); } if (data.toString()[0] == "S") { console.log("Stage Done"); socketServer.emit('Stage',{'key': data.toString()[0]}); } receivedData += data.toString(); //console.log(receivedData); if (receivedData .indexOf('E') >= 0 && receivedData .indexOf('T') >= 0) { sendData = receivedData .substring(receivedData .indexOf('T') + 1, receivedData .indexOf('E')); receivedData = ''; SocketIO_serialemit(sendData); } }); }); } http.listen(3000, function(){ console.log('listening on *:3000') }); app.use(require('express').static(__dirname + '/public')); //sever static files (css, js, fonts, etc.)

答案 1 :(得分:1)

试试这个:

 protected void onPause(){
        super.onPause();
        mySong.release();
        finish();
}