简体   繁体   English

jQuery计时问题? 使用PHP / mssql

[英]jQuery timing issues? Using PHP/mssql

<html>
<script type='text/javascript' src='jquery.js'></script>
<script type='text/javascript'>
<?php 
    // this sets variables sot session for nodeid 
    $nid = 1;//$_GET["nnid"];

    session_start(); 
    $_SESSION['sesnodid']=$nid;
?> 

var array;

$(document).ready(function (){
    $('#itemsOnPageDiv input:checkbox').click(function(event){
        var obj = [];
        $('#itemsOnPageDiv input[type=checkbox]:checked').each(function(index, value){
        obj.push($(this).attr("value"));
        alert($(this).attr("value")+" added to array");
    });
    array = JSON.stringify(obj);
    });
});



$(document).ready(function(){
    $("#cl").click(function(){
    $.post('buildUserPage.php', { array : array }, 
    function(output){
        $('#debug').html(output).show();
    });
    alert(array);
    });
});

</script>
<div id="itemsOnPageDiv">

<?php

include("hawkfunctions.php");

newPageCheckBoxBuider();

$user_page_cell_info = getUserPageCellInfo($nid);

$i = 0;

echo "<script>";
while($i < count($user_page_cell_info)){
    echo "
    alert('Clicking ".$user_page_cell_info[$i]['tag_id']."');
    \$(document).ready(function(){
    \$('#".trim($user_page_cell_info[$i]['tag_id'])."').trigger('click');
    });";
    $i++;
}
echo "</script>";
?>

</div>

<button id="cl">Save</button>

<div id = 'debug'></div>
</html>

OK. 好。 I have a list of checkboxes built from an sql db. 我有一个从sql db构建的复选框列表。 I use php to write jquery that gets the correct 'checked' boxes based on data in the db. 我使用php编写基于数据库中数据获取正确的“选中”框的jquery。 I then have this script 'click' the needed checkboxes. 然后,我使此脚本“单击”所需的复选框。 Each 'click' is then to populate an array. 然后,每次“单击”都将填充一个数组。 This array will contain the data needed from the jquery 'clicked' boxes and any boxes checked or unchecked by the user. 该数组将包含jquery“单击”框中以及用户选中或未选中的所有框所需的数据。 This array is to be used by another php script to change the db based on the changes in what boxes are checked. 另一个php脚本将使用此数组来根据选中的复选框的更改来更改数据库。

Here is the problem: 这是问题所在:

The boxes that are supposed to be checked correctly get checked but. 本应正确检查的框却被选中了。 The array that is supposed to get populated doesn't get correctly populated all the time. 应该填充的数组并非一直都正确填充。 Most noticeably the array is empty if only one of the check boxes is initially checked. 最引人注意的是,如果最初仅选中了一个复选框,则该数组为空。 The user unchecking then checking again seems to fix this but obviously this isn't ideal. 用户先取消选中然后再次选中似乎可以解决此问题,但显然这不是理想的选择。 Sometimes when 2 are checked one of the checkboxes data doesn't enter the array. 有时,当选中2时,复选框数据之一不会进入数组。 Any amount past this seems OK. 超过此金额似乎还可以。

Clicking a checkbox is ambiguous. 单击复选框是不明确的。 Sometimes it works - sometime it doesn't. 有时它可以工作-有时不起作用。 It's an unreliable method of doing it. 这是一种不可靠的方法。 If it's already checked, it's going to uncheck it. 如果它已经被选中,它将取消选中它。

The correct way to do it would be to use the .prop function, or .attr() (see debate here ) 正确的方法是使用.prop函数或.attr() (请参阅此处的辩论)

$('#".trim($user_page_cell_info[$i]['tag_id'])."').prop("checked", true)

While we're at it, here's some tips. 在此过程中,这里有一些提示。

  • Separate your files. 分隔文件。 PHP / HTML / JS / CSS shouldn't be together (for clarity reason) PHP / HTML / JS / CSS不应该在一起(出于清楚的原因)

  • From what I understand.. everytime you click a checkbox, it rebuilds your checkbox JSON... and then do nothing. 据我了解..每当您单击复选框时,它都会重新构建复选框JSON ...,然后什么也不做。 So if I click 3 boxes, it will query 3 times the document to construct the JSON. 因此,如果单击3个框,它将查询3次文档以构造JSON。 You should only do it when someone clicks the $("#cl") button. 仅当有人单击$(“#cl”)按钮时,才应该这样做。

  • Don't name your array "array". 不要将您的数组命名为“ array”。 Try to name it something clearer, like, checkbox_array. 尝试将其命名为更清晰的名称,例如checkbox_array。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM