喜 正如您在上面的图片中看到的,我正在创建一个在我的项目中使用的简单日历。
表格的HTML:
<table>
<tr><th>time</th><th>2012-12-23</th>etc..</tr>
<tr><th>09:00AM</th><td></td><td></td>etc...</tr>
我使用jquery(可选)让那些td可以选择。
我的问题:当用户点击保存时,将表单数据发布到php文件,我怎样才能包含所选td的日期和时间?
例如:我想当用户点击立即保存时(在img上方),它会沿着“2012-12-24 12:00 PM”发送
问题解决了“raina77ow”的答案。
但是从评论和我的评价中我认为我的问题很简短,所以我添加了我的代码以便明确:0
HTML
<FORM id='booking_form'>
A form that u see above in img</form><a id='save'>save</a>
<h1>Week Calendar</h1>
<div class="body">
<table id='schedule'>
<thead>
<tr><th>Time</th><?foreach($cal[0] as $d)echo "<th>$d</th>";//$cal[0]contain week days starting from today?></tr>
</thead>
<?//create our table by nice clean loop
$start_time = "09:00:00";
$end_time = "20:00:00";
while(strtotime($start_time) <= strtotime($end_time)){?>
<tr>
<th><?=date("h:i A", strtotime($start_time));?></th>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<?
$start_time = date("H:i:s", strtotime("$start_time +60 minutes"));
}//end while
?>
<tr></tr>
</table>
单击保存按钮时JS
var datas=$('#booking_form').serialize();
var request = $.ajax({
url: "./booking/add",
type: "POST",
data: datas,
dataType: "html"
});
request.done(function(msg) {$info.html( msg );});
request.fail(function(jqXHR, textStatus) {$info.html( "<div id='error'>" + textStatus +"</div>");});
request.success(function(data){$info.html(data);});
return false;
这是我的问题,如何将所选字段的时间和日期添加到该ajax帖子中。 感谢您的回答,我想出了在#booking_form name = date和name = time中添加2个隐藏的输入,并添加了选择的选项来为它们添加值。
$schedule.selectable({
filter: 'td',
selected: function( event, ui ) {
var time = $(ui.selected).parent().children(':first').text();
date = $(ui.selected).closest('table').children(':first').find('th').eq($(ui.selected).index()).text();
$('#booking_form').find('[name=date]').val(date);
$('#booking_form').find('[name=time]').val(time);
}
});
答案 0 :(得分:2)
获得时间非常容易 - 因为它是点击行的第一个单元格。要获取“日期”列,我们可以使用index
方法。
$('td').click(function() {
var $cell = $(this);
time = $cell.parent().children(':first').text();
date = $cell.closest('table').children(':first').find('th').eq($cell.index()).text();
alert(date + ' ' + time);
});
答案 1 :(得分:2)
试试这个:
$('td').click(function() {
var $td = $(this);
var $th = $td.closest('table').find('th').eq($td.index());
// Get the header text...
alert($th.text());
var $firstTD = $td.closest('tr').find('td:first');
// Get the first td text...
alert($firstTD.text());
});
答案 2 :(得分:1)
只需使用纯JavaScript,一旦拥有<td>
,就可以获得:
var time = td.parentNode.cells[0].firstChild.nodeValue,
date = td.parentNode.parentNode.rows[0].cells[td.cellIndex].firstChild.nodeValue;