【问题标题】:Input value returning undefined输入值返回未定义
【发布时间】:2014-02-07 12:37:10
【问题描述】:

我正在使用 https://github.com/eternicode/bootstrap-datepicker 并希望在日期更改时触发 ajax 调用。

首先要确定日期何时更改,方法是插入 - 插入 changeDate 事件。然后将文本框中的值提醒到屏幕上...

http://bootstrap-datepicker.readthedocs.org/en/latest/events.html#changedate

这是我的小提琴....

http://jsfiddle.net/MangoMM/nY5C6/3/

HTML

<div class="input-group date">
    <input class="form-control" placeholder="Select Date" readonly="1" 
        data-provide="datepicker" data-date-format="yyyy-mm-dd" 
        data-date-start-date="today()" data-date-autoclose="true" 
        data-date-days-of-week-disabled="[2,4,6,0]" 
        data-date-today-highlight="1" name="appointment[date]" 
        type="text" id="appointment[date]" /> 
    <span class="input-group-addon">
        <span class="glyphicon glyphicon-calendar"></span>
    </span>
</div>

jQuery

$(".input-group.date").on('changeDate', function(){
    alert(event.type + " is fired: " + $('#appointment[date]').val());
});

我尝试了各种组合,包括$(this).val() 等,但没有运气...

为什么我的输入值显示为未定义?如何获取文本框中的值?

【问题讨论】:

    标签: jquery input text jquery-events


    【解决方案1】:

    您需要使用两个反斜杠转义[]:\\。

    alert(event.type + " is fired: " + $('#appointment\\[date\\]').val());
    

    DEMO

    来自Docs

    使用任何元字符(例如 !"#$%&'()*+,./:;?@[]^`{|}~ )作为 a 的文字部分名称,必须用两个反斜杠转义:\\.

    另外,您需要将event 传递给事件处理程序

    完整代码

    $(".input-group.date").on('changeDate', function(event){
        alert(event.type + " is fired: " + $('#appointment\\[date\\]').val());
    });
    

    【讨论】:

      【解决方案2】:

      试试这个

      console.log($(".form-control").val());
      

      [] 是一个属性选择器。

      换句话说,$('.form-control[data-provide="datepicker"]').val() 是有效的。

      【讨论】:

        【解决方案3】:

        你使用jsf框架吗? 如果是这样,您可以尝试使用 Firebug 并检查查找元素 ID。

        这个框架经常在真实ID之前生成“form:”。

        你可以试试

        $("#form\\:appointment\\[date\\]").val();
        

        【讨论】:

          【解决方案4】:

          首先您需要将事件传递给函数并转义括号

          $(".input-group.date").on('changeDate', function(event){
             //                                             ^event
              alert(event.type + " is fired: " + $('#appointment\\[date\\]').val());
              //                                                ^^     ^^ escape brackers
          });
          

          DEMO

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-04-17
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多