删除点击值

时间:2013-02-21 23:39:27

标签: javascript html

我想创建一个javascript,点击后会将anotif的值更改为0:

Approved:<?php if($anotif<1){
    echo 0;
} else { 
    echo '<a class="anotif" onClick="validator()" href="?anotif">'.$anotif.'</a>';
} ?><br/>

但我不知道怎么做,考虑到我在javascript中是如此新鲜。我已经搜索了一下,但我找不到答案。

function validator(){
    if(document.anotif.clicked){ // I'm not really sure if this is correct.
        // if clicked, change value to zero
}

更确切地说,我希望它在点击后看起来像这样: 从:   批准:3 至:   批准:0

Approved:<?php if($anotif<1){
    echo 0;
} else { 
    echo '<a class="anotif" onClick="validator()" href="?anotif">'.$anotif.'</a>';
} ?><br/>

此代码将引导我使用?anotif来查看表中数据库中的某些数据。一旦获得批准:

  • 被点击,它必须在我移动到另一页的同时设置为零。

    问题是系统需要刷新才能将其设置为0。

    以下是代码的其余部分:

    $firstname =  getuserfield('txtFname');
    $lastname = getuserfield('txtLname');
    echo 'Hello '.$firstname.' '.$lastname.'.<br/>';
    
    $anotif = 0;
    $dnotif = 0;
    $anotif = $anotif + getuserfield('approved_notif');
    $dnotif = $dnotif + getuserfield('disapproved_notif');
    ?>
    <h3>Notifications:</h3>
    Approved:<?php if($anotif<1){echo 0;} else { echo '<a class="anotif" onClick="validator(this);" href="?anotif">'.$anotif.'</a>';}?><br/> //problem is over here
    <?php 
    if(isset($_GET['anotif'])) {
        $query = "SELECT * FROM hrf_leave WHERE empid = '$empid' AND formStatus = 1 AND checked = 0";
        $query_run = mysql_query($query) or die($query."<br/><br/>".mysql_error());
        echo "<table border=1>
        <tr>
        <th>Type of Leave</th>
        <th>Specific Reason</th>
        <th>Date From</th>
        <th>Date To</th>
        <th>Number of Days</th>
        </tr>";
        echo "<tr>";
        while($record = mysql_fetch_array($query_run)){ 
            $leave_id = $record['leave_id'];
            echo "<td>" . $record['type_of_leave'] . "</td>";
            echo "<td>" . $record['specific_reason'] . "</td>";
            echo "<td>" . $record['date_from'] . "</td>";
            echo "<td>" . $record['date_to'] . "</td>";
            echo "<td>" . $record['num_of_days'] . "</td>";
            echo "</tr>";
            $query2 = "UPDATE hrf_leave SET checked=1 WHERE leave_id = $leave_id";
            if($query_run2 = mysql_query($query2)){ 
                $anotif = 0;
                $query3 = "UPDATE hrms_emp_info SET approved_notif=$anotif WHERE empid = $empid";
                if($query_run3 = mysql_query($query3)){
                }
            }
        }
        echo "</table>";
    }
    ?>
    Disapproved:<?php if($dnotif<1){echo 0;} else { echo '<a href="?dnotif">'.$dnotif.'</a><br/>';} ?> //and here
    <?php
    if(isset($_GET['dnotif'])) {
        $query = "SELECT * FROM hrf_leave WHERE empid = '$empid' AND formStatus = 2 AND checked=0"; 
        $query_run = mysql_query($query);
        echo "<table border=1>
        <tr>
        <th>Type of Leave</th>
        <th>Specific Reason</th>
        <th>Date From</th>
        <th>Date To</th>
        <th>Number of Days</th>
        </tr>";
        echo "<tr>";
        while($record = mysql_fetch_array($query_run)){
            $leave_id = $record['leave_id'];
            echo "<td>" . $record['type_of_leave'] . "</td>";
            echo "<td>" . $record['specific_reason'] . "</td>";
            echo "<td>" . $record['date_from'] . "</td>";
            echo "<td>" . $record['date_to'] . "</td>";
            echo "<td>" . $record['num_of_days'] . "</td>";
            echo "</tr>";
            $query2 = "UPDATE hrf_leave SET checked=1 WHERE leave_id = $leave_id";
            if($query_run2 = mysql_query($query2)){ 
                $dnotif = $dnotif-1;
                $query3 = "UPDATE  hrms_emp_info SET disapproved_notif=$dnotif WHERE empid = $empid";
                if($query_run3 = mysql_query($query3)){ 
                }
            }
        }
        echo "</table>";
    }
    
  • 1 个答案:

    答案 0 :(得分:2)

    你可以改变你的锚点:

    <a class="anotif" onClick="return validator(this)" href="?anotif">
    

    然后像这样更改validator()函数:

    function validator(element)
    {
        // element is clicked already, so you can change its value
    
        element.innerHTML = '0';
    
        // prevent click from changing page
        return false;
    }
    

    但是,真正的问题是页面将更改为?anotif,无论你改变什么都会浪费精力。 更新了代码,以防止点击更改页面,只是将数字减少为“0”。