簡體   English   中英

JavaScript函數僅運行一次

[英]Javascript function runs only once

在一個php類中,我正在定義一個表,該表顯示了需要聯系的客戶列表。 在第7列中,我集成了一個復選框,因此當用戶呼叫客戶端時,他/她會選中該復選框。

問題是JS函數第一次運行,然后第二次運行,它說:default.php?page = _PhoneBankingP1:1未被捕獲的ReferenceError:未定義contact(…)

下面列出了正在使用的模塊:

public function phonebank($townCode,$streetCode){           
            $query  =   "SELECT clientId, clientFirstname1, clientLastname1, clientAddress, ";
            $query  .=  "       clientMailshot, clientPhone1, clientMobile1, clientContacted, min(clientDoB) ";
            $query  .=  "FROM _clients ";
            $query  .=  "WHERE  _clients.streetCode = '{$streetCode}' and ";
            $query  .=  "       _clients.townCode   = '{$townCode}' and ";
            $query  .=  "       _clients.GE = 'Y' ";
            $query  .=  "GROUP BY clientAddress ";
            $result =   $this->db->query($query);


            $output = "";
            if ( $result->num_rows > 0 ){
                $output .=  "<div class='alert alert-info'><strong>Information!</strong> All the residents shown below have been extracted from the last Electoral Register.</div>";
                $output .=  "<table class='table table-striped' style='font-size:10pt;' id='myTable' >";
                $output .=      "<thead>";
                $output .=          "<tr>";
                $output .=              "<th>ID #</th>";
                $output .=              "<th>Name</th>";
                $output .=              "<th>Address</th>";
                $output .=              "<th>T</th>";
                $output .=              "<th>Phone</th>";
                $output .=              "<th>Mobile</th>";
                $output .=              "<th class='text-center'>Contacted</th>";
                $output .=          "</tr>";
                $output .=      "</thead>";
                $output .=      "<tbody>";


                while ( $record = mysqli_fetch_array($result, MYSQLI_ASSOC)){
                    $output .=  "<tr>";
                    $output .=      "<td><a href='default.php?page=_clientDetails&id=".$record['clientId']."&mode=edit' style='color: #000;'><span class='pb-clientId'>".$record['clientId']."</span></a></td>"; 
                    $output .=      "<td><span class='pb-fullname'>".$record['clientFirstname1']." ".$record['clientLastname1']."</span></td>";
                    $output .=      "<td>".$record['clientAddress']."</td>";
                    $output .=      "<td>".$record['clientMailshot']."</td>";
                    $output .=      "<td>".$record['clientPhone1']."</td>";
                    $output .=      "<td>".$record['clientMobile1']."</td>";

                    // Makes a checkbox selected
                    if ( $record['clientContacted'] == 'Y'){
                        $optContacted = ' checked ';
                    } else {
                        $optContacted = '';
                    }

                    //$output .=    "<td class='text-center' ><button id='btn-contacted-".$record['clientId']."' onclick='street.clientContacted(&quot;{$record['clientId']}&quot;,&quot;{$record['clientContacted']}&quot;)' class='btn btn-success'>Contacted</button></td>";

                    $output         .=  "<td align='center'>";
                    $output         .=      "<input type='checkbox' id='col7-".$record['clientId']."'  onclick='contacted(&quot;".$record['clientId']."&quot;);' value='1' ".$optContacted." />";
                    $output         .=  "</td>";


                    $output .=  "</tr>";
                }

                $output .=      "</tbody>";
                $output .=  "</table>";
                $output .=  "<br/>";

                echo $output;

            } else {
                echo "No Clients Found in this street";
            }
        }       

然后,更新MYSQL所需的測試JS函數為:

function contacted(id) {
var clientId    =   id;
var col7        =   "col7-"+clientId;
var col7value   =   $("#"+col7).is(':checked');
var data        =   id+"\n"+col7+"\n"+col7value;

alert(data);

//Read checkbox state
if (col7value =='false'){
    contacted = 'N';
} else {
    contacted = 'Y';
}   

$.ajax({
    type:       "POST",
    url:        "_backend/_core/_database/update_Phonebank.php",
    data:       {
        "id":           clientId,
        "contacted":    contacted
    },
    dataType:   "text",
    success:    function(data){
    }
})  
}

如果您能幫助我確定第二次不讀取該功能,將不勝感激。

您定義一個函數:

function contacted(id) {
    //...
}

但是該函數中,您使用一個值覆蓋了該函數:

contacted = 'N';

因此,下一次您嘗試調用contacted()您將嘗試調用字符串,就好像它是一個函數一樣。

為您的變量指定唯一的名稱:

var wasContacted = '';
//...
wasContacted = 'N';
// etc.

這樣,您就不會覆蓋不想覆蓋的內容。

此外,使用var關鍵字聲明變量將在特定范圍內(例如在該函數內)定義變量,而不僅僅是將其放在window對象上。 (您可以在不同的作用域中使用相同名稱的變量,而不會互相影響。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM