2017-08-27 11 views
1

JSFiddleでは動作しますが、notepad ++では動作しません。どうして ? 最初のボタンのみが動作しますが、2番目のボタンは動作しません。私はそれをユニークにするためにIDを変更しようとしましたが、それはまた仕事をしませんでした。また、クラスも仕事をしなかった。ここに何か間違いはありますか?誰かが私を助けることができる?これを解決するのに役立つ人が本当に必要です。事前に私はここにボタンがあり、jdfiddleでそれをやってみることができますが、HTMLではありません

JSコードをありがとう:

function getDate() { 

    var sd = document.getElementById('start_date').value; 
    var d = document.getElementById('duration').value; 




    var date = new Date(sd); 
    var newdate = new Date(date); 
    var durations = parseInt(d); 

    newdate.setDate((newdate.getDate() + durations)-1); 


    var dd = newdate.getDate(); 
    var mm = newdate.getMonth()+1; 
    var y = newdate.getFullYear(); 

    var someFormattedDate = y + '-' + mm + '-' + dd; 
    document.getElementById('end_date').value = someFormattedDate; 




} 

PHPコード:

& sme_id = & PC_ID =」にenctype = "multipart/form-データ">

<tr> 
    <th style="font-family:century gothic"><strong><font color="black">Major</font></strong></td> 
    <th style="font-family:century gothic"><strong><font color="black">Stage</font></strong></td> 
    <th style="font-family:century gothic"><strong><font color="black">Task</font></strong></td> 
    <th style="font-family:century gothic"><strong><font color="black">Task Mark</font></strong></td> 
    <th style="font-family:century gothic"><strong><font color="black">Marking</font></strong></td> 
    <th style="font-family:century gothic"><strong><font color="black">Destination Marking</font></strong></td> 
    <th style="font-family:century gothic"><strong><font color="black">Days</font></strong></td> 
    <th style="font-family:century gothic"><strong><font color="black">Duration</font></strong></td> 
    <th style="font-family:century gothic"><strong><font color="black">Start</font></strong></td> 
    <th style="font-family:century gothic"><strong><font color="black">Finish</font></strong></td> 
    <th></th> 
</tr> 
</thead> 
<tbody> 
<? 
    //GET DATA FROM PROJECT_SCHEDULE 
    /*$sql ="SELECT task_name,task_mark,marking,destination_marking,start_date,end_date FROM project_detail_new 
      WHERE stage_id='1'";*/ 

    /*$sql = "SELECT ps.ps_id,ps.pc_id,ps.major_id,pms.major_name,ps.stage_id,psl.stage_name,ps.task_id,pst.task_name, 
      ps.task_mark,ps.marking,ps.destination_marking,ps.days,ps.duration,ps.start_date,ps.end_date,ps.sys_id,ps.agency_id 
      FROM project_schedule ps 
      LEFT JOIN project_major_stage pms 
      ON ps.major_id=pms.major_id 
      LEFT JOIN project_stage_list psl 
      ON ps.stage_id=psl.stage_id 
      LEFT JOIN project_stage_task pst 
      ON ps.task_id=pst.task_id 
      WHERE ps.pc_id='$pc_id'";*/ 
    $sql= "SELECT DISTINCT(ps.major_id),pms.major_name 
      FROM project_schedule ps 
      LEFT JOIN project_major_stage pms 
      ON ps.major_id=pms.major_id 
      WHERE ps.pc_id='$pc_id' "; 

    $select = mysql_query($sql); 
    $i= 1; 
    while($list =mysql_fetch_array($select)) 
    { 

     $major_name=$list['major_name']; 
     $major_id=$list['major_id']; 
?> 
<tr> 
    <td style ="font-family:century gothic"colspan="12"><strong><font color="black"><?echo $list['major_name'];?></font></strong></td> 
</tr> 

<? 
    $sqla= "SELECT DISTINCT (ps.stage_id),psl.stage_name,pms.major_name 
      FROM project_schedule ps 
      LEFT JOIN project_stage_list psl 
      ON ps.stage_id=psl.stage_id 
      LEFT JOIN project_major_stage pms 
      ON ps.major_id=pms.major_id 
      WHERE ps.pc_id=1 AND pms.major_id='$major_id'"; 

    $selecta=mysql_query($sqla); 
    while($lista =mysql_fetch_array($selecta)) 
    { 

     $stage_name=$lista['stage_name']; 
     $stage_id=$lista['stage_id']; 
?> 
<tr> 
<td></td> 
<td style="font-family:centruy-gothic" colspan="10"<strong><font color="black"><?echo $lista['stage_name'];?></font></strong></td> 
</tr> 


<? 
    $sqlb ="SELECT ps.*,pst.task_name,psl.stage_name 
      FROM project_schedule ps 
      LEFT JOIN project_stage_task pst 
      ON ps.task_id=pst.task_id 
      LEFT JOIN project_stage_list psl 
      ON ps.stage_id=psl.stage_id 
      WHERE ps.pc_id=1 AND psl.stage_id='$stage_id'"; 

    $selectb =mysql_query($sqlb); 

    while($listb=mysql_fetch_array($selectb)) 
    { 

     $ps_id=$listb['ps_id']; 
     $task_name=$listb['task_name']; 
     $task_mark=$listb['task_mark']; 
     $marking=$listb['marking']; 
     $destination_marking=$listb['destination_marking']; 
     $days=$listb['days']; 
     $duration=$listb['duration']; 
     $start_date=$listb['start_date']; 
     $end_date=$listb['end_date']; 


?> 

<!----THIS IS THE PHP CODE TO BE USED TO CALCULATE THE END DATE BASED ON START DATE ENTERED AND DURATION PICKED FROM DATABASE--> 
<tr> 
<td></td> 
<td></td> 

<td style="font-family:century-gothic"> 
    <strong> 
    <font color="black"> 
     <?echo $listb['task_name'];?> 


    </font> 
    </strong> 
</td> 

<td> 
    <input type="text" id="task_mark1" class="task_mark" name="task_mark" value="<? echo $listb['task_mark'];?>" /> 
</td> 

<td> 
    <input type="text" id="marking1" class="marking" name="marking" value="<? echo $listb['marking'];?>" /> 
</td> 

<td> 
    <input type="text" id="destination_marking1" class="destination_marking" name="destination_marking" value="<? echo $listb['destination_marking'];?>" /> 
</td> 

<td> 
    <input type="text" id="days1" class="days" name="days" value="<? echo $listb['days'];?>" /> 
</td> 

<td> 
    <input type="text" id="duration1" class="duration" name="duration" value="<? echo $listb['duration'];?>" /> 
</td> 

<td> 
    <input type="text" id="start_date1" class="start_date" name="start_date" value="<? echo $listb['start_date'];?>" /> 
</td> 

<td> 
    <input type="text" id="end_date1" class="end_date" name="end_date" value="<? echo $listb['end_date'];?>" /> 
</td> 

<td align="center"> 

    <input type="button" id ="btn1" value="GENERATE" name="action" class ="submit-green" onclick="getDate(this.id)"/> 

</td> 

</tr> 

<!---------------------------------------------------------------------------------------------------------------------------------------> 
<td> 
    <input type="text" id="task_mark2" class="task_mark" name="task_mark" value="<? echo $listb['task_mark'];?>" /> 
</td> 

<td> 
    <input type="text" id="marking2" class="marking" name="marking" value="<? echo $listb['marking'];?>" /> 
</td> 

<td> 
    <input type="text" id="destination_marking2" class="destination_marking" name="destination_marking" value="<? echo $listb['destination_marking'];?>" /> 
</td> 

<td> 
    <input type="text" id="days2" class="days" name="days" value="<? echo $listb['days'];?>" /> 
</td> 

<td> 
    <input type="text" id="duration2" class="duration" name="duration" value="<? echo $listb['duration'];?>" /> 
</td> 

<td> 
    <input type="text" id="start_date2" class="start_date" name="start_date" value="<? echo $listb['start_date'];?>" /> 
</td> 

<td> 
    <input type="text" id="end_date2" class="end_date" name="end_date" value="<? echo $listb['end_date'];?>" /> 
</td> 

<td align="center"> 

    <input type="button" id ="btn2" value="GENERATE" name="action" class ="submit-green" onclick="getDate(this.id)"/> 

</td> 

</tr> 
<? 
    }}} 
?> 

<tr> 
    <td colspan=5 align="center"><input class="submit-green" type="submit" value="Save" /></td>  
</tr> 
</form> 
+0

メモ帳で++それを解決するためにどのようにWebブラウザ –

+0

ではないので、 'tsは' ++ JSFiddleではなく、メモ帳で働いていました? –

+0

IDが重複しており、すべての要素にユニークであると考えられているため、機能しません。あなたの質問にHTMLコードを修正して、要素を異なるIDで区別することができます。 – Nicholas

答えて

0

function getDate(id) { 
 
    id = id.match(/\d+/); 
 
    var sd = document.getElementById('start_date'+ id).value; 
 
    var d = document.getElementById('duration' + id).value; 
 
    var date = new Date(sd); 
 
    var newdate = new Date(date); 
 
    var durations = parseInt(d); 
 
    newdate.setDate((newdate.getDate() + durations)-1); 
 
    var dd = newdate.getDate(); 
 
    var mm = newdate.getMonth()+1; 
 
    var y = newdate.getFullYear(); 
 
    var someFormattedDate = y + '-' + mm + '-' + dd; 
 
    document.getElementById('end_date' + id).value = someFormattedDate; 
 
}
<table> 
 
    <thead> 
 
    <tr> 
 
     <th style="font-family:century gothic"> 
 
     <strong><font color="black">Major</font></strong></th> 
 
     <th style="font-family:century gothic"><strong><font color="black">Stage</font></strong></th> 
 
     <th style="font-family:century gothic"><strong><font color="black">Task</font></strong></th> 
 
     <th style="font-family:century gothic"><strong><font color="black">Task Mark</font></strong></th> 
 
     <th style="font-family:century gothic"><strong><font color="black">Marking</font></strong></th> 
 
     <th style="font-family:century gothic"><strong><font color="black">Destination Marking</font></strong></th> 
 
     <th style="font-family:century gothic"><strong><font color="black">Days</font></strong></th> 
 
     <th style="font-family:century gothic"><strong><font color="black">Duration</font></strong></th> 
 
     <th style="font-family:century gothic"><strong><font color="black">Start</font></strong></th> 
 
     <th style="font-family:century gothic"><strong><font color="black">Finish</font></strong></th> 
 
     <th></th> 
 
    </tr> 
 
    </thead> 
 
    <tbody> 
 
    <? 
 
//GET DATA FROM PROJECT_SCHEDULE 
 
/*$sql ="SELECT task_name,task_mark,marking,destination_marking,start_date,end_date FROM project_detail_new 
 
WHERE stage_id='1'";*/ 
 

 
/*$sql = "SELECT ps.ps_id,ps.pc_id,ps.major_id,pms.major_name,ps.stage_id,psl.stage_name,ps.task_id,pst.task_name, 
 
ps.task_mark,ps.marking,ps.destination_marking,ps.days,ps.duration,ps.start_date,ps.end_date,ps.sys_id,ps.agency_id 
 
FROM project_schedule ps 
 
LEFT JOIN project_major_stage pms 
 
ON ps.major_id=pms.major_id 
 
LEFT JOIN project_stage_list psl 
 
ON ps.stage_id=psl.stage_id 
 
LEFT JOIN project_stage_task pst 
 
ON ps.task_id=pst.task_id 
 
WHERE ps.pc_id='$pc_id'";*/ 
 
$sql= "SELECT DISTINCT(ps.major_id),pms.major_name 
 
FROM project_schedule ps 
 
LEFT JOIN project_major_stage pms 
 
ON ps.major_id=pms.major_id 
 
WHERE ps.pc_id='$pc_id' "; 
 

 
$select = mysql_query($sql); 
 
$i= 1; 
 
while($list =mysql_fetch_array($select)) 
 
{ 
 

 
$major_name=$list['major_name']; 
 
$major_id=$list['major_id']; 
 
?> 
 
    <tr> 
 
     <td style ="font-family:century gothic" colspan="12"> 
 
     <strong> 
 
      <font color="black"> 
 
      <? echo $list['major_name'];?> 
 
      </font> 
 
     </strong> 
 
     </td> 
 
    </tr> 
 

 
    <? 
 
$sqla= "SELECT DISTINCT (ps.stage_id),psl.stage_name,pms.major_name 
 
FROM project_schedule ps 
 
LEFT JOIN project_stage_list psl 
 
ON ps.stage_id=psl.stage_id 
 
LEFT JOIN project_major_stage pms 
 
ON ps.major_id=pms.major_id 
 
WHERE ps.pc_id=1 AND pms.major_id='$major_id'"; 
 

 
$selecta=mysql_query($sqla); 
 
while($lista =mysql_fetch_array($selecta)) 
 
{ 
 

 
$stage_name=$lista['stage_name']; 
 
$stage_id=$lista['stage_id']; 
 
?> 
 
    <tr> 
 
     <td></td> 
 
     <td style="font-family:centruy-gothic" colspan="10"> 
 
     <strong> 
 
      <font color="black"> 
 
      <?echo $lista['stage_name'];?> 
 
      </font> 
 
     </strong> 
 
     </td> 
 
    </tr> 
 

 

 
    <? 
 
$sqlb ="SELECT ps.*,pst.task_name,psl.stage_name 
 
FROM project_schedule ps 
 
LEFT JOIN project_stage_task pst 
 
ON ps.task_id=pst.task_id 
 
LEFT JOIN project_stage_list psl 
 
ON ps.stage_id=psl.stage_id 
 
WHERE ps.pc_id=1 AND psl.stage_id='$stage_id'"; 
 

 
$selectb =mysql_query($sqlb); 
 

 
while($listb=mysql_fetch_array($selectb)) 
 
{ 
 

 
$ps_id=$listb['ps_id']; 
 
$task_name=$listb['task_name']; 
 
$task_mark=$listb['task_mark']; 
 
$marking=$listb['marking']; 
 
$destination_marking=$listb['destination_marking']; 
 
$days=$listb['days']; 
 
$duration=$listb['duration']; 
 
$start_date=$listb['start_date']; 
 
$end_date=$listb['end_date']; 
 

 

 
?> 
 

 
    <!----THIS IS THE PHP CODE TO BE USED TO CALCULATE THE END DATE BASED ON START DATE ENTERED AND DURATION PICKED FROM DATABASE--> 
 
    <tr> 
 
     <td></td> 
 
     <td></td> 
 

 
     <td style="font-family:century-gothic"> 
 
     <strong> 
 
      <font color="black"> 
 
      <?echo $listb['task_name'];?> 
 
      </font> 
 
     </strong> 
 
     </td> 
 
     <td> 
 
     <input type="text" id="task_mark1" class="task_mark" name="task_mark" value="<? echo $listb['task_mark'];?>" /> 
 
     </td> 
 
     <td> 
 
     <input type="text" id="marking1" class="marking" name="marking" value="<? echo $listb['marking'];?>" /> 
 
     </td> 
 
     <td> 
 
     <input type="text" id="destination_marking1" class="destination_marking" name="destination_marking" value="<? echo $listb['destination_marking'];?>" /> 
 
     </td> 
 
     <td> 
 
     <input type="text" id="days1" class="days" name="days" value="<? echo $listb['days'];?>" /> 
 
     </td> 
 
     <td> 
 
     <input type="text" id="duration1" class="duration" name="duration" value="<? echo $listb['duration'];?>" /> 
 
     </td> 
 
     <td> 
 
     <input type="text" id="start_date1" class="start_date" name="start_date" value="<? echo $listb['start_date'];?>" /> 
 
     </td> 
 
     <td> 
 
     <input type="text" id="end_date1" class="end_date" name="end_date" value="<? echo $listb['end_date'];?>" /> 
 
     </td> 
 
     <td align="center"> 
 
     <input type="button" id ="btn1" value="GENERATE" name="action" class ="submit-green" onclick="getDate(this.id)"/> 
 
     </td> 
 
    </tr> 
 

 
    <!---------------------------------------------------------------------------------------------------------------------------------------> 
 
    <td> 
 
     <input type="text" id="task_mark2" class="task_mark" name="task_mark" value="<? echo $listb['task_mark'];?>" /> 
 
    </td> 
 
    <td> 
 
     <input type="text" id="marking2" class="marking" name="marking" value="<? echo $listb['marking'];?>" /> 
 
    </td> 
 
    <td> 
 
     <input type="text" id="destination_marking2" class="destination_marking" name="destination_marking" value="<? echo $listb['destination_marking'];?>" /> 
 
    </td> 
 
    <td> 
 
     <input type="text" id="days2" class="days" name="days" value="<? echo $listb['days'];?>" /> 
 
    </td> 
 
    <td> 
 
     <input type="text" id="duration2" class="duration" name="duration" value="<? echo $listb['duration'];?>" /> 
 
    </td> 
 
    <td> 
 
     <input type="text" id="start_date2" class="start_date" name="start_date" value="<? echo $listb['start_date'];?>" /> 
 
    </td> 
 
    <td> 
 
     <input type="text" id="end_date2" class="end_date" name="end_date" value="<? echo $listb['end_date'];?>" /> 
 
    </td> 
 
    <td align="center"> 
 
     <input type="button" id ="btn2" value="GENERATE" name="action" class ="submit-green" onclick="getDate(this.id)"/> 
 
    </td> 
 
    </tr> 
 
<? 
 
}}} 
 
?> 
 

 
<tr> 
 
    <td colspan=5 align="center"> 
 
    <input class="submit-green" type="submit" value="Save" /> 
 
    </td>  
 
</tr> 
 
</form>

次のようにそれを行うことができます...などを1,2,3から端の数を増やす異なるように、テーブルの行ごとにすべてのあなたのIDを作成します。例btn1、btn2など。次にHTMLコードでonclick = "getDate()"をonclick = "getDate(this.id)"に変更します。そして、あなたのコードをいくつか変更して、以下のjavascriptコードを使用してください。

Javascriptコード:

function getDate(id) { 
    id = id.match(/\d+/); 
    var sd = document.getElementById('start_date'+ id).value; 
    var d = document.getElementById('duration' + id).value; 
    var date = new Date(sd); 
    var newdate = new Date(date); 
    var durations = parseInt(d); 
    newdate.setDate((newdate.getDate() + durations)-1); 
    var dd = newdate.getDate(); 
    var mm = newdate.getMonth()+1; 
    var y = newdate.getFullYear(); 
    var someFormattedDate = y + '-' + mm + '-' + dd; 
    document.getElementById('end_date' + id).value = someFormattedDate; 
} 

HTMLコード:

<table> 
    <thead> 
    <tr> 
     <td colspan=11 align="center"><input class="submit-green" type="submit" value="Save" /></td> 
    </tr> 
    <tr> 
     <td style="font-family:century gothic"><strong><font color="black">Major</font></strong></td> 
     <td style="font-family:century gothic"><strong><font color="black">Stage</font></strong></td> 
     <td style="font-family:century gothic"><strong><font color="black">Task</font></strong></td> 
     <td style="font-family:century gothic"><strong><font color="black">Task Mark</font></strong></td> 
     <td style="font-family:century gothic"><strong><font color="black">Marking</font></strong></td> 
     <td style="font-family:century gothic"><strong><font color="black">Destination Marking</font></strong></td> 
     <td style="font-family:century gothic"><strong><font color="black">Days</font></strong></td> 
     <td style="font-family:century gothic"><strong><font color="black">Duration</font></strong></td> 
     <td style="font-family:century gothic"><strong><font color="black">Start</font></strong></td> 
     <td style="font-family:century gothic"><strong><font color="black">Finish</font></strong></td> 
    </tr> 
    </thead> 
    <tbody> 
    <tr> 
     <td style="font-family:century gothic" colspan="12"><strong><font color="black">Initial</font></strong></td> 
    </tr> 
    <tr> 
     <td></td> 
     <td style="font-family:century gothic" colspan="11"><strong><font color="black">Prelim</font></strong></td> 
    </tr> 
    <tr> 
     <td></td> 
     <td></td> 
     <td style="font-family:century gothic"><strong><font color="black">Side Rail</font></strong></td> 

     <!----THIS IS THE PHP CODE TO BE USED TO CALCULATE THE END DATE BASED ON START DATE ENTERED AND DURATION PICKED FROM DATABASE--> 
     <td> 
     <input type="text" id="task_mark1" class="task_mark" value="" /> 
     </td> 
     <td> 
     <input type="text" id="marking1" class="marking" value="" /> 
     </td> 
     <td> 
     <input type="text" id="destination_marking1" class="destination_marking" value="" /> 
     </td> 
     <td> 
     <input type="text" id="days1" class="days" value="" /> 
     </td> 
     <td> 
     <input type="text" id="duration1" class="duration" value="" /> 
     </td> 
     <td> 
     <input type="text" id="start_date1" class="start_date" value="" /> 
     </td> 
     <td> 
     <input type="text" id="end_date1" class="end_date" value="" /> 
     </td> 

     <td align="center"> 

     <input type="button" id="btn1" value="GENERATE" class="btnGenerate" onclick="getDate(this.id)" /> 
     </td> 
    </tr> 
    <tr> 
     <td></td> 
     <td></td> 
     <td style="font-family:century gothic"><strong><font color="black">Bina Stor</font></strong></td> 

     <td> 
     <input type="text" id="task_mark2" class="task_mark" name="task_mark" value="" /> 
     </td> 
     <td> 
     <input type="text" id="marking2" class="marking" name="marking" value="" /> 
     </td> 
     <td> 
     <input type="text" id="destination_marking2" class="destination_marking" name="destination_marking" value="" /> 
     </td> 
     <td> 
     <input type="text" id="days2" class="days" name="days" value="" /> 
     </td> 
     <td> 
     <input type="text" id="duration2" class="duration" name="duration" value="" /> 
     </td> 
     <td> 
     <input type="text" id="start_date2" class="start_date" name="start_date" value="" /> 
     </td> 
     <td> 
     <input type="text" id="end_date2" class="end_date" name="end_date" value="" /> 
     </td> 
     <td align="center"> 
     <input type="button" id="btn2" value="GENERATE" class="btnGenerate" onclick="getDate(this.id)" /> 
     </td> 
    </tr> 
    <tr> 
     <td colspan=5 align="center"><input class="submit-green" type="submit" value="Save" /></td> 
    </tr> 
    </tbody> 
</table> 
+0

データベースからフェッチするデータを実装しようとしましたが、まだ動作しません。ここに間違いはないの? –

+0

私はそこに私の編集した –

+0

あなたのHTMLコードには、正しく閉じられていない要素がいくつかあるので、間違いがありました。また、要素を開いて、で閉じたヘッドテーブルにもあります。私は自分の答えを編集し、作業用のコードスニペットを含んでいます。私はPHPコードをそのまま残して、スニペットから自分のHTMLコードを使用して動作するかどうかを今すぐ試してみてください。 – adda82

関連する問題