繁体   English   中英

如何在外部jquery.js文件中获取PHP迭代变量?

[英]How to get a PHP iteration variable in an external jquery.js file?

我正在编写一个显示用户的待办事项列表的PHP程序。 我所拥有的基本上是一个无序列表,具有一个复选框,当选中该复选框时,该复选框将允许用户将列表项标记为已完成(即,给文本加上删除线)。 这是我的清单代码

echo '<ul>';

for ($i=0; $i<6; $i++){

       $text = "This is item number " . $i;
       $complete = 'No';
       $order = 'This item is to be done #' . $i;

       echo '<li id = '. $i . '>';

    echo 'Item complete? <input type="checkbox" id="checkbox" />';
    echo '<span id = ' . $i . ' onLoad="crossOut()">Item: ' . $text . ' Complete? ' .$complete . '&nbsp&nbspWhen to do Item: ' . $order . '</span>';
    echo '</li>';

       }

echo '</ul>';


}

这是我正在使用的jQuery函数

$(document).ready(function crossOut(){
    $("#checkbox").change(function crossOutText(){
        if($(this).is(":checked")){
            $("#liID").css("text-decoration", "line-through");
        }
    })
})

我要弄清楚的是如何将列表ID从PHP传递到外部JS文件中的jquery函数,以便每当用户检查项目时,它都会将该列表项目标记为已完成并在文本上加上删除线该列表项。 我是使用jQuery的新手,任何人愿意提供的帮助将不胜感激。

$(document).ready(function(){
    $("input:checkbox").change(function(){
        if($(this).is(":checked")){
            $(this).parents("li").css("text-decoration", "line-through");
            // ^^^^^^^^^^^^^^ strike through the parent list item.
        }
    })
})

这是使用CSS类的更好的方法:

$(document).ready(function(){
    $("input:checkbox").change(function(){
        $(this).parents("li").toggleClass('strike', this.checked)
        // ^^^^^^^^^^^^^^ strike through the parent list item.
    })
})

CSS:

.strike {
    text-decoration: line-through;
}

演示: http//jsfiddle.net/maniator/unmLd/


免责声明:
在两个示例中,我都将#checkbox更改为input:checkbox ,因为您不能有多个具有相同ID的元素! 尝试改用类。

另外, 删除代码的crossout()部分...它什么也不做,可能会在页面上引发错误...

像这样吗

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="js/jquery.js"></script>
<script>
$(document).ready(function(){
    $('input[type="checkbox"]').change(function(){
        if($(this).is(":checked")){
            $(this).parent().css("text-decoration", "line-through");
        }else{
            $(this).parent().css("text-decoration", "none");
        }
    });
});
</script>
<title>Untitled Document</title>
</head>

<body>

<?php
    echo '<ul>';
    for ($i=0; $i<6; $i++){
        $text = "This is item number " . $i;
        $complete = 'No';
        $order = 'This item is to be done #' . $i;
        echo '<li id = '. $i . '>';
        echo 'Item complete? <input type="checkbox" id="checkbox" />';
        echo '<span id = ' . $i . '>Item: ' . $text . ' Complete? ' .$complete . '&nbsp&nbspWhen to do Item: ' . $order . '</span>';
        echo '</li>';
    }
    echo '</ul>';
?>

</body>
</html>

暂无
暂无

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

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