javascript - 使用 JqueryUI Mobile ListView 替代超链接

标签 javascript php jquery html css

我有一个使用 JqueryUI Mobile 的页面。更具体地说,它使用 JqueryMobile 列表

http://demos.jquerymobile.com/1.2.1/docs/lists/lists-ul.html

我喜欢 ListView 的外观、感觉和用法,但我遇到了问题。

用户需要能够快速点击链接,这对我现在的做法来说很难。该链接执行 PHP 函数,然后重定向回它们所在的页面,然后循环重新开始。现在我对 AJAX 越来越熟悉了,我希望能够让他们单击链接,它会更新数据库,然后调用我的 AJAX 脚本来更新数据而无需刷新页面。

这将使用户能够更快地完成任务,因为他们目前的大部分停机时间是刷新(或更准确地说是重定向回)页面所需的几秒钟。如果可能的话,他们希望能够每秒甚至更多地点击一个链接。

我的问题是:如何根据从 MySQLi 数据库动态生成的链接执行数据库更新而不需要用户刷新?我相信,一旦有了它,我还可以使用 Ajax 每四分之一左右更新一次列表。我考虑过使用按钮,但不确定如何将它与 ListView 联系起来,因为 ListView 似乎仅基于链接。

旁注 - 他们的 Ajax 标准实践是否应该经常更新?有什么我应该遵循的指导方针吗?

这是我当前代码的示例:

<?php
session_start();
if(isset($_SESSION['username']))
{

}
else
{
$_SESSION['error']="You are logged in.";
header('Location: index.php');
    exit;
}

?><!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0"/> <!--320-->
<link rel="stylesheet" href="js/jquery.mobile-1.4.5.min.css">
<script src="js/jquery-1.11.3.min.js"></script>
<link rel="stylesheet" href="style.css">
<script src="js/jquery.mobile-1.4.5.min.js"></script>
<style>

.split-custom-wrapper {
    /* position wrapper on the right of the listitem */
    position: absolute;
    right: 0;
    top: 0;
    height: 100%;
}

.split-custom-button {
    position: relative;
    float: right;   /* allow multiple links stacked on the right */
    height: 80%;
    margin:10px;
    min-width:3em;
    /* remove boxshadow and border */
    border:none;
    moz-border-radius: 0;
    webkit-border-radius: 0;
    border-radius: 0;
    moz-box-shadow: none;
    webkit-box-shadow: none;
    box-shadow: none;
}

.split-custom-button span.ui-btn-inner {
    /* position icons in center of listitem*/
    position: relative;
    margin-top:50%;
    margin-left:50%;
    /* compensation for icon dimensions */
    top:11px; 
    left:-12px;
    height:40%; /* stay within boundaries of list item */
}

.ui-icon-delete:after{
    background-color: #B22222 !important;

background-image:url("data:image/svg+xml;charset=US-ASCII,%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22iso-8859-1%22%3F%3E%3C!DOCTYPE%20svg%20PUBLIC%20%22-%2F%2FW3C%2F%2FDTD%20SVG%201.1%2F%2FEN%22%20%22http%3A%2F%2Fwww.w3.org%2FGraphics%2FSVG%2F1.1%2FDTD%2Fsvg11.dtd%22%3E%3Csvg%20version%3D%221.1%22%20id%3D%22Layer_1%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20xmlns%3Axlink%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink%22%20x%3D%220px%22%20y%3D%220px%22%20%20width%3D%2214px%22%20height%3D%2214px%22%20viewBox%3D%220%200%2014%2014%22%20style%3D%22enable-background%3Anew%200%200%2014%2014%3B%22%20xml%3Aspace%3D%22preserve%22%3E%3Cpolygon%20fill%3D%22%23FFF%22%20points%3D%2214%2C3%2011%2C0%207%2C4%203%2C0%200%2C3%204%2C7%200%2C11%203%2C14%207%2C10%2011%2C14%2014%2C11%2010%2C7%20%22%2F%3E%3Cg%3E%3C%2Fg%3E%3Cg%3E%3C%2Fg%3E%3Cg%3E%3C%2Fg%3E%3Cg%3E%3C%2Fg%3E%3Cg%3E%3C%2Fg%3E%3Cg%3E%3C%2Fg%3E%3Cg%3E%3C%2Fg%3E%3Cg%3E%3C%2Fg%3E%3Cg%3E%3C%2Fg%3E%3Cg%3E%3C%2Fg%3E%3Cg%3E%3C%2Fg%3E%3Cg%3E%3C%2Fg%3E%3Cg%3E%3C%2Fg%3E%3Cg%3E%3C%2Fg%3E%3Cg%3E%3C%2Fg%3E%3C%2Fsvg%3E")
}

.ui-icon-home:after{
    background-color: #A2CD5A !important;

}
.ui-icon-arrow-u-r:after{
    background-color: #3D59AB !important;

}
</style>
</header>


  </head><center>  <h2 style="">Empty For now<br><br>
    </h2></center>
<a href="home.php" class="ui-btn ui-icon-home ui-btn-icon-left" data-ajax='false'>HOME</a>
<a href="ViewOrderMobile.php" class="ui-btn ui-icon-edit ui-btn-icon-left" data-ajax='false'>VIEW / EDIT CURRENT LINE</a>
<br><br><br><br>
<center><center>
  <div data-role="main" class="ui-content"style="margin-top:-75px;">

    <h2 style=""></h2>
    <ul data-role="listview">
  <?php 
include "../../includes/databaseconnections/demo/database_connection.php";
///////////////////////////////////////////////////////////////////////////////////////////////
/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
else{}
$query = "SELECT * FROM Table1 LEFT JOIN Table2 USING (ID) WHERE Table1.feild1 = '0' ORDER BY dateSelected ASC LIMIT 25";
if ($result = mysqli_query($link, $query)) {
    /* fetch associative array */
    while ($row = mysqli_fetch_assoc($result)) {
        if ($row['photoLink'] == NULL)
        {
            $row['photoLink'] = "endofgroup";
            $row['lastName'] = "End Of Group " ;
            $ID = "&ID=".$row['ID'];
        }
        if ($row[leftGym] == "1") { $flash = "style='color:#B22222;font-size:140%'";} else {$flash ="";}
            echo "<li><a href='button1.php?sid=${row['ID']}' $flash  style='font-size:140%;' width='25px' data-ajax='false'>&nbsp;  &nbsp;  {$row["lastName"]}, {$row["firstName"]} ({$row["pmBusNumber"]})</a><div class='split-custom-wrapper'>

            <a href='button2.php?sID={$row['ID']}&lane=1{$ID}'  data-role='button' class='split-custom-button' data-icon='delete' data-rel='dialog' data-theme='c'  data-ajax='false' data-iconpos='notext'></a>           
        </div></li>";
    }
    /* free result set */
    mysqli_free_result($result);
}
 mysqli_close($link);

 /////////////////////////////////////////////////////////////////////////////////
?>
    </ul><br>

</div> 
 </div>

 </div>

  </body>

</html>

最佳答案

我已经在代码片段中添加了注释。如果您需要更多信息,请告诉我们。 基于 jquery ajax() v3.1.1

html:用 class=myCustomClass 更新列表中的 a 元素

<a href='button1.php?sid=${row['ID']}' $flash  style='font-size:140%;' width='25px' data-ajax='false' class='myCustomClass'>&nbsp;  &nbsp;  {$row["lastName"]}, {$row["firstName"]} ({$row["pmBusNumber"]})</a>
<div class='split-custom-wrapper'>
<a href='button2.php?sID={$row['ID']}&lane=1{$ID}'  data-role='button' class='myCustomClass split-custom-button' data-icon='delete' data-rel='dialog' data-theme='c'  data-ajax='false' data-iconpos='notext'>
</a>           
</div>
<span id="status"></span>

脚本:

$(function () {

    //Attach the click event to Links only with class=myCustomClass and perform this function
     $("a[class=myCustomClass]").on('click', function (e) {
        e.preventDefault(); //preven the page from navigating, the default behaviour for a link
        $.ajax({
            url : this.href, //perform a ajax request with the link, GET in this case
            /* type: POST, */
            beforeSend : function () {
                $("#status").text('Working..')
            }
        }).done(function (data) {
            console.log(data); //do something with the data if any
        }).fail(function (jqXHR, textStatus, errorThrown) {
            console.log("ERROR"); //report in console for errors
            console.info(jqXHR);
            console.info(textStatus);
            console.info(errorThrown);
        }).always(function () {
            //do this step every time 
            $("#status").text('completed..')
            console.info("completed"); irrespective of result
        });
    })

关于javascript - 使用 JqueryUI Mobile ListView 替代超链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41250584/

相关文章:

jquery - 不带 Handlebars 的预输入自定义模板

javascript - 如果验证失败,如何停止表单提交

javascript - .bind() 在 javascript 中不返回 native 代码

javascript - 带有两个输入字段的 SweetAlert 提示

javascript - Backbonejs 集合更改排序顺序?

php - 在 paypal 通话期间未注意到 NOTIFYURL?

javascript - 如何将原型(prototype)函数添加到初始化函数中的事件监听器?

php - 在 php 中比较 2 个图像

PHP 包含() : File size & performance

javascript - ajax响应后无法遍历对象