如何使这些按钮运行不同?

时间:2017-08-09 08:38:40

标签: javascript php

我已生成按钮了。它们的功能是相同的,当我点击它时,它将进行计算。问题是当我点击第二个生成按钮时,它将执行第一个生成按钮的计算,同样转到第三个按钮,依此类推。我想要的是,每个生成按钮只会为自己的工作做计算。谁能帮我 ?

my generate button

以下是代码:

<tr>
    <td></td>
    <td></td>
    <td style="font-family:centruy-gothic"<strong><font color = "black">
    <?echo $listb['task_name'];?>
    <input type="hidden" name="pc_id[]" value="<? echo $pc_id;?>" >
    <input type="hidden" name="ps_id[]" value="<? echo $ps_id;?>" >
    </font></strong></td>
    <td align="center" style="font-family:century gothic"><input type="text" id ="task_mark" class="input-medium" name="task_mark[]" value="<? echo $listb['task_mark'];?>" onchange="getDate()"/></td>
    <td align="center" style="font-family:century gothic"><input type="text" id="marking" class="input-medium" name="marking[]" value="<? echo $listb['marking'];?>"onchange="getDate()"/></td>
    <td align="center" style="font-family:century gothic"><input type="text" id="destination_marking" class="input-medium" name="destination_marking[]" value="<? echo $listb['destination_marking'];?>"onchange="getDate()"/></td>
    <td align="center" style="font-family:century gothic"><input type="text" id="days" class="input-medium" name="days[]" value="<? echo $listb['days'];?>"onchange="getDate()"/></td>
    <td align="center" style="font-family:century gothic"><input type="text" id="duration" class="input-medium" name="duration[]" value="<? echo $listb['duration'];?>" onchange="calc()"/></td>
    <td align="center" style="font-family:century gothic"><input type="text" id="start_date" class="input-long" name="start_date[]" value="<? echo $listb['start_date'];?>" class="date demo" onchange="getDate()" ></td>
    <td align="center" style="font-family:century gothic"><input type="text" id="end_date" class="input-long" name="end_date[]" value="<? echo $listb['end_date'];?>" class="date demo" onchange="getDate()"></td>
    <td align="center" ><input class="submit-green" id="end_date" type="button" name="end_date[]" value="GENERATE" onclick="getDate()"></td>

    </tr>

这是脚本

<script>
        function getDate()

    {   

        var sd = document.getElementById('start_date').value;
        var dura = document.getElementById('duration').value;
        var m = document.getElementById('marking').value;

        var date = new Date(sd);
        var newdate = new Date (date);
        var durations = parseInt(dura);
        var markings = parseInt(m);

        if (markings === 1)
        {
            newdate.setDate(newdate.getDate());
        }
        else if (markings === 2)
        {
            newdate.setDate(newdate.getDate() + durations);
        }
        else if (markings === 3)
        {
            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;

    }

    </script>

2 个答案:

答案 0 :(得分:1)

你可以在html代码中给出一个参数来修改onchange="getDate()",如下所示:

<td align="center" style="font-family:century gothic"><input type="text" id ="task_mark" class="input-medium" name="task_mark[]" value="<? echo $listb['task_mark'];?>" onchange="getDate(1)"/></td>
<td align="center" style="font-family:century gothic"><input type="text" id="marking" class="input-medium" name="marking[]" value="<? echo $listb['marking'];?>"onchange="getDate(2)"/></td>
<td align="center" style="font-family:century gothic"><input type="text" id="destination_marking" class="input-medium" name="destination_marking[]" value="<? echo $listb['destination_marking'];?>"onchange="getDate(3)"/></td>
<td align="center" style="font-family:century gothic"><input type="text" id="days" class="input-medium" name="days[]" value="<? echo $listb['days'];?>"onchange="getDate(4)"/></td>
<td align="center" style="font-family:century gothic"><input type="text" id="duration" class="input-medium" name="duration[]" value="<? echo $listb['duration'];?>" onchange="calc()"/></td>
<td align="center" style="font-family:century gothic"><input type="text" id="start_date" class="input-long" name="start_date[]" value="<? echo $listb['start_date'];?>" class="date demo" onchange="getDate(5)" ></td>
<td align="center" style="font-family:century gothic"><input type="text" id="end_date" class="input-long" name="end_date[]" value="<? echo $listb['end_date'];?>" class="date demo" onchange="getDate(6)"></td>
<td align="center" ><input class="submit-green" id="end_date" type="button" name="end_date[]" value="GENERATE" onclick="getDate(7)"></td>

然后,在脚本中,您可以使用参数执行不同的操作:

<script>
        function getDate(param)
    { 

现在你现在看了他检查的按钮

答案 1 :(得分:1)

我假设你的表行之前有foreach。 在这种情况下,对于每个输入字段,您有几次相同的id。

尝试使id唯一。像这样:

<input type="text" id ="task_mark_row1">

//下一个表格行:

<input type="text" id ="task_mark_row2">

...