如何使用变量

时间:2016-01-20 11:48:16

标签: php jquery variables jquery-selectors

我有一个div类,其中包含一个从jquery脚本更改html的boxcatID。问题是我的特定页面有很多这些div,所以我的脚本当然会影响所有这些div,只会影响它们。

所以为了解决这个问题,我在类名中添加了一个catID变量,例如boxcatID + catID,例如来自boxcatID1。

现在在我的jquery脚本中,我需要解决我正在处理的特定div,因此它可能是boxcatID1甚至是boxcatID30。所以我把它作为jquery:

 $(".boxcatID"+catID).html( singleValues + glyphLeft); 

但这会引发错误

 Error: Syntax error, unrecognized expression: .boxcatID1;

这些div是从我的数据库中的循环创建的。所以30条记录意味着30个div。

 <div class="boxcatID<?php echo $cat_id; ?>">TEXT HERE</div>

那么如果我想在这里更改TEXT,我怎么能解决这个单一的div?

以下是原始代码:

glyphLeft = '<span class="glyphicon glyphicon-user" data-unicode="e008"></span>';
            function displayValsL() {

                catID = dp(this).attr("catID");
                console.log("catID is " + catID);
                var singleValues = dp( "select.selectbox-listL" ).val();
                var boxcat = '#boxcatID'+catID;

                dp("#boxcatID"+catID).html( singleValues + glyphLeft);
                var pic = "images/players/" + dp('option:selected' , this).attr("id") + ".png";
                dp(".img-left").attr("src", pic);
                nameValue1 = dp('option:selected' , this).val();
                enableButton();
            }
            dp( "select.selectbox-listL" ).change( displayValsL );

PHP

<div class="selectbox-wrapper left">
                    <div class="selectboxTopL" id="boxcatID<?php echo $cat_id; ?>" catID='<?php echo $cat_id; ?>'>
                        Player 1 <span class="glyphicon glyphicon-user" data-unicode="e008"></span>
                    </div> <!-- END selectboxTop -->
                    <select  class="selectbox-listL" catID='<?php echo $cat_id; ?>' size="6">

                        <?php
                            $query2 = "SELECT first_name,last_name, playerID, categoryID FROM Players WHERE categoryID = '$cat_id' ORDER BY last_name";
                            $result2 = mysqli_query($conn, $query2);

                            if($result2 === FALSE) { 
                                die(mysql_error()); // TODO: better error handling
                            }

                            //fetch tha data from the database
                            while ($row2 = mysqli_fetch_array($result2)) {

                                $playerID = $row2['playerID'];
                                $playerFN = $row2['first_name'];
                                $playerLN = $row2['last_name'];
                                $playerCatID = $row2['categoryID'];

                                echo '<option id="'. $playerFN . $playerLN .'" value="' . $playerFN ." ". $playerLN . '">' . $playerFN . " " . $playerLN . '</option>';
                            }
                        ?>
                    </select>
                </div><!-- END selectbox-wrapper -->

1 个答案:

答案 0 :(得分:0)

问题是因为您的HTML的;属性中有一个尾随class,因此选择器不匹配。您只需要移动PHP代码块中的;即可。悬挂'也可以删除。

改变这个:

<div class="boxcatID<?php echo $cat_id ?>;"'>TEXT HERE</div>

到这个

<div class="boxcatID<?php echo $cat_id; ?>">TEXT HERE</div>

更好的是,通过从引发此代码运行的事件的元素遍历DOM来消除对增量类的需求。