簡體   English   中英

使用jQuery獲取文本字段的值(文本文件包含日期)

[英]Get the value of text field (text fidle contains date) using jquery

我想在文本字段中獲取日期的值。 我的HTML代碼:

 <table id="sample">
    <tr>
      <td>
          Date
     </td>
   </tr>
 </table>

js文件:

  $("#sample tr td").after('<td><input type="text" id="datepicker"/></td>').queue(function() {
    $('#datepicker').datepicker();
      $(this).dequeue();
 });

我想提醒輸入文本框的日期。 的jsfiddle:

http://jsfiddle.net/9r9r8krr/

提醒更改值,例如:

$(document).on("change","input", function(){
    if($(this).val()!="") {
      alert($(this).val());
    }
});

DEMO

您可以在選擇器的任何操作中使用.val()來選擇datepicker元素以獲取其值。

同樣不是after()方法是同步方法,因此不需要使用queue()

$("#sample tr td").after('<td><input type="text" id="datepicker"/></td>');
$('#datepicker').datepicker();

$('button').click(function(){
    alert($('#datepicker').val())
})

演示: 小提琴

//In below code, you can replace input[type=text] with classname/id of input field. 
$('input[type=text]').on('change', function() {  
    alert($('input[type=text]').val());
});

 $("#sample tr td").after('<td><input type="text" id="datepicker"/></td>').queue(function() { $('#datepicker').datepicker({ onSelect: function(date) { alert("You selected : " + date); } }); $(this).dequeue(); }) 
 <link rel="stylesheet" href="https://code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.11.3/jquery-ui.min.js"></script> <table id="sample"> <tr> <td> Date </td> </tr> 

我沒有完全明白這個問題,但我想你是這個意思!!

 $("#sample tr td").after('<td><input type="text" id="datepicker"/></td>'); $('#datepicker').datepicker({ onSelect: function(date) { alert("You selected : " + date); } }); 
 <link rel="stylesheet" href="https://code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.11.3/jquery-ui.min.js"></script> <table id="sample"> <tr> <td> Date </td> </tr> 

這也將起作用...

試試這個...為我工作!

$(document).ready(function(){
$(document).on("change","input#datepicker", function(){
    if($(this).val()!="") {
      alert($(this).val());
    }
}); });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM