2012-01-24 9 views
0

私は、クリックして日付をクリックすると、カレンダーを表示するために次のコードを使用しました。javascriptカレンダーのバグ

// JavaScript Document 
//script for date picker 

// You can edit this function if you need the date in a different format 
function format_date_field(y, m, d) { 
    return '' + y + '-' + 
      ((m > 9) ? m : '0' + m) + '-' + 
      ((d > 9) ? d : '0' + d); 
} 



m_list = Array("January", "February", "March", 
       "April", "May",  "June", 
       "July", "August", "September", 
       "October", "November", "December");  
//target = ''; 
targets = Array(); 

function set_date_field(y, m, d, t_idx) { 
    eval(targets[t_idx] + ' = unescape(\'' + escape(format_date_field(y, m, d)) + '\')'); 
} 

function show_cal(target_a) { 
    var d = new Date(); 
    show_cal_m(d.getFullYear(), d.getMonth(), target_a); 
} 

function show_cal_m(y,m, target_a) { 
    t_idx = targets.push(target_a) - 1; 
    write_cal_pop(y,m, open('about:blank','cal_win'+t_idx,'width=175,height=150'), t_idx); 
} 

function write_cal_pop(y,m, cal_win, t_idx) { 
    cal_win.document.open(); 
    cal_win.document.writeln('<html><head><title>Date Picker</title>'); 
    cal_win.document.writeln('<style type="text/css">'); 
    cal_win.document.writeln('td {'); 
    cal_win.document.writeln(' font-family: Verdana, Arial, Helvetica, sans-serif;'); 
    cal_win.document.writeln(' font-size: 9pt;'); 
    cal_win.document.writeln(' color: #000000;'); 
    cal_win.document.writeln(' background-color: #CCCCCC;'); 
    cal_win.document.writeln(' text-align: center;'); 
    cal_win.document.writeln('}'); 
    cal_win.document.writeln('th {'); 
    cal_win.document.writeln(' font-family: Verdana, Arial, Helvetica, sans-serif;'); 
    cal_win.document.writeln(' font-size: 9pt;'); 
    cal_win.document.writeln(' color: #000000;'); 
    cal_win.document.writeln(' background-color: #CC0000;'); 
    cal_win.document.writeln(' text-align: center;'); 
    cal_win.document.writeln('}'); 
    cal_win.document.writeln('a {'); 
    cal_win.document.writeln(' color: #000000;'); 
    cal_win.document.writeln(' text-decoration: none;'); 
    cal_win.document.writeln('}'); 
    cal_win.document.writeln('</style></head><body leftmargin=2 topmargin=2 marginwidth=2 marginheight=2>'); 


    var one_day  = 1000 * 60 * 60 * 24; 
    //var next_month = (m==11) ? 0 : m + 1; 

    d = new Date(y,m,1, 0,0,0,0); 
    d = new Date(d.valueOf() - (one_day * 6)); 

    while (d.getDay() != 1) { 
     d = new Date(d.valueOf() + one_day); 
    } 

    cal_win.document.writeln('<table align="center" width="100%" height="100%">'); 
    cal_win.document.writeln('<tr>'); 

    //PREV 
    cal_win.document.write('<th><a href="javascript: opener.write_cal_pop('); 
    if (m==0) { cal_win.document.write((y-1) + ', 11'); } 
    else  { cal_win.document.write(y + ', ' + (m-1)); } 
    cal_win.document.writeln(', self, ' + t_idx + ');">&lt;</a></td>'); 

    //CUR. 
    cal_win.document.writeln('<th colspan=5><b>' + m_list[m] + ' ' + y + '</b></td>'); 

    //NEXT 
    cal_win.document.write('<th><a href="javascript: opener.write_cal_pop('); 
    if (m==11) { cal_win.document.write((y+1) + ', 0'); } 
    else  { cal_win.document.write(y + ', ' + (m+1)); } 
    cal_win.document.writeln(', self, ' + t_idx + ');">&gt;</a></td>'); 

    cal_win.document.writeln('</tr>'); 

    //day of week header 
    cal_win.document.writeln('<tr><th>M</th><th>T</th><th>W</th><th>T</th><th>F</th><th>S</th><th>S</th></tr>'); 

    //DAYS 
    day_count = 0; 
    while (day_count < 42) { 
     day_count++ 

     if (d.getDay() == 1) { cal_win.document.writeln('<tr>'); } 
     cal_win.document.writeln('<td>' + (d.getMonth()==m ? '<b>' : '')); 
     cal_win.document.writeln('<a href="javascript: opener.set_date_field(' + 
       d.getFullYear() + ', ' + (d.getMonth()+1) + ', ' + d.getDate() + 
       ', ' + t_idx + '); self.close();">' + d.getDate() + '</a>'); 

     cal_win.document.writeln(d.getMonth()==m ? '</b></td>' : '</td>'); 
     //next 
     //d = new Date(d.valueOf() + one_day); //replaced to stop double day in oct error 
     d.setDate(d.getDate() + 1); 
     //if (d.getMonth() == next_month && d.getDay() == 1) { break; } 
    } 


    cal_win.document.writeln('</table>'); 
    cal_win.document.writeln('</body></html>'); 
    cal_win.document.close(); 
} 

私のPHPページでは、次のコードを使用しました。

<td><input type="text" name="joining" id="joining"/> 
        <a href="javascript: show_cal('document.admission_form.joining.value')">pick date</a></td> 
      </tr> 

私がピック日付リンクをクリックすると、新しいウィンドウが開きます。同じページの入力欄の近くでカレンダーを開く必要があります。どうやってやるの?

+0

window.openを必要としない新しいカレンダーを見つける!たくさんのプラグインを選びます。 – epascarello

答えて

0

(hrefのアンカーを参照してください)基本的には最初の変更は、その値は、ページへのアンカーだと宣言するために「#」に変更されたのhrefタグである

<td><input type="text" name="joining" id="joining"/> 
       <a href="#" onclick="show_cal('document.admission_form.joining.value')">pick date</a></td> 
     </tr> 

<td><input type="text" name="joining" id="joining"/> 
       <a href="javascript: show_cal('document.admission_form.joining.value')">pick date</a></td> 
     </tr> 

を交換してくださいこれは進行していないので冗長リンクである。 2つ目は、カレンダー表示機能をイベントリスナーに移動することです。

+0

まだ動作しません – designersvsoft

0

この行は、ページが新しいウィンドウで開くように引き起こしているあなたは、新しいウィンドウを開くか、その名前のウィンドウがすでに存在する場合、上書きされますオープン()コマンドを呼び出している

write_cal_pop(y,m, open('about:blank','cal_win'+t_idx,'width=175,height=150'), t_idx); 

それの内容。あなたは現在のDOM内のDOM要素にコンテンツを記述する必要がある(これはあなたがカレンダーを表示するために使用されるページで指定したdiv要素である可能性があります。)

0

非常に速く、JavaScriptの上ラフ修正( jQueryの+日付ピッカーを使用した方がよい、例えば、http://jqueryui.com/demos/datepicker/) 変更機能 "show_cal"、 "show_cal_m" および "set_date_field" に示すように追加機能を追加します。

function set_date_field(y, m, d, t_idx) { 
    targets[t_idx].value = format_date_field(y, m, d); 
} 

function show_cal(obj, target_a) { 
    var d = new Date(); 
    show_cal_m(obj, d.getFullYear(), d.getMonth(), target_a); 
} 

function show_cal_m(obj, y,m, target_a) { 
    t_idx = targets.push(target_a) - 1; 
    var ifr = document.createElement('iframe'); 
    ifr.style.width = '175px'; 
    ifr.style.height = '150px'; 
    ifr.style.position = 'absolute'; 
    var pos = findPos(obj); 
    ifr.style.top = pos[1]; 
    ifr.style.left = pos[0]; 
    ifr.id = 'calendar'; 
    document.body.appendChild(ifr); 
    ifr = (ifr.contentWindow) ? ifr.contentWindow : (ifr.contentDocument.document) ? ifr.contentDocument.document : ifr.contentDocument; 
    write_cal_pop(y,m, ifr, t_idx); 
} 

function closeCalendar() { 
    document.body.removeChild(document.getElementById('calendar'));  
} 

function findPos(obj) { 
    var curleft = curtop = 0; 
    if (obj.offsetParent) { 
     curleft = obj.offsetLeft 
     curtop = obj.offsetTop 
     while (obj = obj.offsetParent) { 
      curleft += obj.offsetLeft 
      curtop += obj.offsetTop 
     } 
    } 
    return [curleft,curtop]; 
} 

コードを変更:

cal_win.document.writeln('<a href="javascript: parent.set_date_field(' + 
       d.getFullYear() + ', ' + (d.getMonth()+1) + ', ' + d.getDate() + 
       ', ' + t_idx + '); parent.closeCalendar();">' + d.getDate() + '</a>'); 

と電話をかける

<a href='#' onclick="show_cal(this, document.admission_form.joining); return false;">pick date</a> 
関連する問題