首页 \ 问答 \ 在document.ready中声明的函数是未定义的?(function declared inside document.ready is undefined?)

在document.ready中声明的函数是未定义的?(function declared inside document.ready is undefined?)

如果我在document.ready中声明了一个函数,我得到一个错误。 喜欢这个

$(document).ready(function(){
    function updateSizeOptions()
    {
        alert("updateSizeOptions");
    }

    var jGrid = $("#list_main");
    jGrid.jqGrid({
        url:'db.php?ajaxOp=getData',
            colModel:[
                $.extend(true,
                { name:'shape_id'
                  ,index:'shape_id'
                  ,edittype:'select'
                  ,formatter:'select'
                  ,editoptions: { onclick:"javascript:updateSizeOptions();" }
                }
                ,{}
            ]
        ....
});

它将给出错误:“ReferenceError:未定义updateSizeOptions”。
但如果我移动document.ready外的函数,一切正常。
喜欢这个

function updateSizeOptions()
{
    console.debug("updateSizeOptions");
}

$(document).ready(function(){
    var jGrid = $("#list_main");
....

为什么?


If I declare a function inside document.ready, I get an error. Like this

$(document).ready(function(){
    function updateSizeOptions()
    {
        alert("updateSizeOptions");
    }

    var jGrid = $("#list_main");
    jGrid.jqGrid({
        url:'db.php?ajaxOp=getData',
            colModel:[
                $.extend(true,
                { name:'shape_id'
                  ,index:'shape_id'
                  ,edittype:'select'
                  ,formatter:'select'
                  ,editoptions: { onclick:"javascript:updateSizeOptions();" }
                }
                ,{}
            ]
        ....
});

It will give Error : "ReferenceError: updateSizeOptions is not defined".
But If I moved the function outside document.ready, everything works fine.
Like this

function updateSizeOptions()
{
    console.debug("updateSizeOptions");
}

$(document).ready(function(){
    var jGrid = $("#list_main");
....

WHY ?


原文:https://stackoverflow.com/questions/13455811
更新时间:2021-07-12 16:07

最满意答案

您可以根据$multiple_names_array的大小制作占位符。 您不应该像在代码中那样直接将值放入查询中,因为(如注释中所述)您很容易受到SQL注入的影响。 我也将所有的值放入一个数组中。

这是您正在寻找的解决方案。 所有值都绑定

//Assuming the names are separated by commas, e.g "John,Jack,Jill';
$multiple_names = $_POST['multiple_names'];  
$multiple_names_array = explode(',',$multiple_names);

$placeholders = implode(', ',  array_fill(0, count($multiple_names_array), '?'));

//Put all values into one array.
$arguments = array_merge(array(), $multiple_names_array);
array_push($arguments, $checkInDateFinal);
array_push($arguments, $checkOutDateFinal);

// SELECT * FROM attendance WHERE name IN (?, ?, ?, ?, etc...) AND today_date BETWEEN ? AND ? ORDER BY id ASC
$stmt1 = $conn->prepare("SELECT * from attendance where name in ($placeholders) and today_date between ? and ? order by id ASC");
$stmt1->execute($arguments);

You can make placeholders depending on the size of $multiple_names_array. You should never put values directly into a query like in your code because (as said in the comments) you are vulnerable to SQL injection. I would also put all of the values into one array.

This is the solution you are looking for. All values are binded.

//Assuming the names are separated by commas, e.g "John,Jack,Jill';
$multiple_names = $_POST['multiple_names'];  
$multiple_names_array = explode(',',$multiple_names);

$placeholders = implode(', ',  array_fill(0, count($multiple_names_array), '?'));

//Put all values into one array.
$arguments = array_merge(array(), $multiple_names_array);
array_push($arguments, $checkInDateFinal);
array_push($arguments, $checkOutDateFinal);

// SELECT * FROM attendance WHERE name IN (?, ?, ?, ?, etc...) AND today_date BETWEEN ? AND ? ORDER BY id ASC
$stmt1 = $conn->prepare("SELECT * from attendance where name in ($placeholders) and today_date between ? and ? order by id ASC");
$stmt1->execute($arguments);

相关问答

更多
  • 你实际上是在存储它们。 您列出的代码是将它们存储在数组数组中以查看它们,您可以使用以下命令修改for : foreach ($result_array as $key => $val) { echo "$key = " . print_r($val, true) . "\n"; } $ val实际上是一个包含行的所有字段的数组。 You are actually storing them. The code you have listed is storing them in array of ...
  • 您可以根据$multiple_names_array的大小制作占位符。 您不应该像在代码中那样直接将值放入查询中,因为(如注释中所述)您很容易受到SQL注入的影响。 我也将所有的值放入一个数组中。 这是您正在寻找的解决方案。 所有值都绑定 。 //Assuming the names are separated by commas, e.g "John,Jack,Jill'; $multiple_names = $_POST['multiple_names']; $multiple_names_arra ...
  • 这是代码,将$ sword转换为数组并在第二个查询中使用它: $symbol = array("abc", "def", "ghi"); $sword = array(); $sql = "SELECT * FROM around"; $results = $conn->query($sql); foreach($results as $row) { $stop = preg_replace("/[0-9]/", "", $row['tip']); if (in_array($stop, $ ...
  • 在一个数据库字段中放置一个ID数组并不是一个好主意,并且通过这种方式设计数据库会损失关系数据库为您提供的一些功能。 我会创建第三个表,而不是在产品表中存储成分ID数组。 示例表模式如下所示: Products id productName productDescription Ingredients id ingredientName ProductIngredients id id_Products id_Ingredients 一些示例数据可能如下所示: Produ ...
  • 你应该这样做: $sql = "SELECT DISTINCT Branch FROM student_main"; $result = mysql_query($sql); echo ""; echo "
  • 猜测你有特定于操作系统的行结尾绊倒了你。 试试preg_split $lines = preg_split("/\\n|\\r|\\r\\n/", $str); Guessing you've got OS specific line endings tripping you up. Try preg_split $lines = preg_split("/\\n|\\r|\\r\\n/", $str);
  • 如果您的GET参数与您的数据库字段匹配,您可以像这样构建您的查询字符串: $field_array = array('president', 'congress', 'nomination_received_by_senate', 'state'); $query = 'SELECT `name` FROM `nominations` WHERE '; $conditions = array(); foreach($field_array as $field) { $value = $_GET[$fi ...
  • 您可能希望首先反序列化数据库中的数据。 一旦它在PHP数组中,你可以检查这些选项,所以说: if(in_array("5",$array)) { $q=mysql_query("SELECT id from table WHERE options=5")or die(mysql_error()); } 如果您需要对所有选项进行查询,可以执行如下循环: foreach($array as $option) { $q=mysql_query("SELECT id from table WHERE op ...
  • 首先,正如之前的评论员所说,你应该使用PDO而不是mysql_ *查询。 在这里阅读PDO。 当您使用SELECT查询从DB获取数据时,您将获得数组。 我建议你使用PDO 文档中的 fetchAll()。 因此,您的目标是将此数据保存在某个变量中。 就像你用$row 。 之后,您需要遍历每个数组并获取您的数据: foreach($row as $r) { //We do this to access each of ours athlete data $adjustedscore= $row[$r]["di ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)