【问题标题】:Javascript function runs only onceJavascript 函数只运行一次
【发布时间】:2016-12-27 14:04:23
【问题描述】:

在一个 php 类中,我定义了一个表格,其中显示了需要联系的客户列表。在第 7 列中,我集成了一个复选框,因此当用户致电客户端时,他/她会勾选此复选框。

问题是JS函数第一次运行,然后第二次运行它说:default.php?page=_PhoneBankingP1:1 Uncaught ReferenceError: contacted is not defined(...)

使用的模块如下:

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){
    }
})  
}

如果您能帮我确定我的函数没有被第二次读取,我将不胜感激。

【问题讨论】:

  • 尝试提供minimal reproducible example。很难看出您的示例中发生了什么。 PHP是相关的吗?您不能提供它生成的 HTML 的精简版本吗?
  • 如果某个答案解决了您的问题,请考虑接受该答案。以下是meta.stackexchange.com/questions/5234/… 然后返回此处并对勾号/复选标记执行相同操作直到它变为绿色的方法。这通知社区,找到了解决方案。否则,其他人可能会认为问题仍然悬而未决,可能想要发布(更多)答案。您将获得积分,并鼓励其他人帮助您。 欢迎来到 Stack!

标签: javascript php


【解决方案1】:

你定义一个函数:

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

但是那个函数中,你用一个值覆盖这个函数:

contacted = 'N';

因此,下次您尝试调用 contacted() 时,您会尝试调用一个字符串,就好像它是一个函数一样。

给你的变量起唯一的名字:

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

这样你就不会覆盖你不想覆盖的东西。

此外,使用var 关键字声明变量将在特定范围内(例如在该函数内)定义它们,而不是仅仅将它们放在window 对象上。 (您可以在不同的范围内拥有相同名称的变量,而不会相互影响。)

【讨论】:

  • @Jonasw:可选,但我同意在这种情况下肯定会推荐它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-28
  • 1970-01-01
  • 2018-07-06
  • 2012-04-01
  • 1970-01-01
相关资源
最近更新 更多