dsxfa26482 2014-04-30 15:55
浏览 83
已采纳

为什么在使用默认值打印字段时会打印空字符串?

There is some weird thing happening. Following html with jquery generates a time picker. If I submit the default values to actiont.php a blank string gets printed by the script.

Why is that ? (Here is the jsfiddle with the output of jquery and html)

HTML:

<html>
<head> 

    <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
    <script src="jquery.timepicker.js" type="text/javascript"></script>
    <title> </title>    

</head>
<body>
    <form action="actiont.php" method="post">
        <input type="text" class="timepicker" name="timepicker" id="timepicker" /> <br />
        <input type="submit" />
    </form>
</body>

JQuery:

    /* jQuery timepicker
 * replaces a single text input with a set of pulldowns to select hour, minute, and am/pm
 *
 * Copyright (c) 2007 Jason Huck/Core Five Creative (http://www.corefive.com/)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * Version 1.0
 */

(function($){
    jQuery.fn.timepicker = function(){
        this.each(function(){
            // get the ID and value of the current element
            var i = this.id;
            var v = $(this).val();

            // the options we need to generate
            var hrs = new Array('01','02','03','04','05','06','07','08','09','10','11','12');
            var mins = new Array('00','15','30','45');
            var ap = new Array('am','pm');

            // default to the current time
            var d = new Date;
            var h = d.getHours();
            var m = d.getMinutes();
            var p = (h >= 12 ? 'pm' : 'am');

            // adjust hour to 12-hour format
            if(h > 12) h = h - 12;

            // round minutes to nearest quarter hour
            for(mn in mins){
                if(m <= parseInt(mins[mn])){
                    m = parseInt(mins[mn]);
                    break;
                }
            }

            // increment hour if we push minutes to next 00
            if(m > 45){
                m = 0;

                switch(h){
                    case(11):
                        h += 1;
                        p = (p == 'am' ? 'pm' : 'am');
                        break;

                    case(12):
                        h = 1;
                        break;

                    default:
                        h += 1;
                        break;
                }
            }

            // override with current values if applicable
            if(v.length == 7){
                h = parseInt(v.substr(0,2));
                m = parseInt(v.substr(3,2));
                p = v.substr(5);
            }

            // build the new DOM objects
            var output = '';

            output += '<select id="h_' + i + '" class="h timepicker">';             
            for(hr in hrs){
                output += '<option value="' + hrs[hr] + '"';
                if(parseInt(hrs[hr]) == h) output += ' selected';
                output += '>' + hrs[hr] + '</option>';
            }
            output += '</select>';

            output += '<select id="m_' + i + '" class="m timepicker">';             
            for(mn in mins){
                output += '<option value="' + mins[mn] + '"';
                if(parseInt(mins[mn]) == m) output += ' selected';
                output += '>' + mins[mn] + '</option>';
            }
            output += '</select>';              

            output += '<select id="p_' + i + '" class="p timepicker">';             
            for(pp in ap){
                output += '<option value="' + ap[pp] + '"';
                if(ap[pp] == p) output += ' selected';
                output += '>' + ap[pp] + '</option>';
            }
            output += '</select>';              

            // hide original input and append new replacement inputs
            $(this).attr('type','hidden').after(output);
        });

        $('select.timepicker').change(function(){
            var i = this.id.substr(2);
            var h = $('#h_' + i).val();
            var m = $('#m_' + i).val();
            var p = $('#p_' + i).val();
            var v = h + ':' + m + p;
            $('#' + i).val(v);
        });

        return this;
    };
})(jQuery);

$(document).ready(function() {
   $('.timepicker').timepicker();
});


/* SVN: $Id: jquery.timepicker.js 456 2007-07-16 19:09:57Z Jason Huck $ */

actiont.php

<?php
  echo $_POST["timepicker"];
?>
  • 写回答

1条回答 默认 最新

  • douzi9430 2014-04-30 16:08
    关注

    Easiest solution would be to trigger change event once plugin is initialized:

    $(document).ready(function() {
        $('.timepicker').timepicker();
        $('select.timepicker').change();
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?