我有一个数组'name'。我需要检查这个数组中的元素是否出现在html中的列表中。
php中的代码如下:foreach ($name as $value2) {
echo "<script>
if($('li:not(:contains('"".$value2."'"))'))
{
alert('".$value2." does not exists');
}
else
{
alert('".$value2." exists');
}
</script>";
}
但是我得到的警报值不存在于每个元素。
你可以使用PHP数组:
<?php
$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)) {
echo "Got Irix";
}
if (in_array("mac", $os)) {
echo "Got mac";
}
?>
参考你可以这样尝试:
<?php foreach ($name as $value2) { ?>
<script>
if($('li:not(:contains('"" <?php echo $value2; ?>"'"))'))
{
alert('"<?php echo $value2; ?>" does not exists');
}
else
{
alert('"<?php echo $value2; ?>" exists');
}
</script>
<?php } ?>
假设您无法在服务器上测试html的内容(例如,在ajax或其他东西之后),那么创建
不是好的做法<script>
.
</script>
<script>
.
</script>
<script>
.
</script>
当你可以做
$jsString = implode("','", $array);
$jsString = "'".$jsString."'";
?>
<script>
var vals = [<? echo $jsString; ?>];
var contains =[],notContains=[],liContains=[];
$.each(vals,function(i,val) {
var li = $('li:contains("'+val+'")');
if (li) {
contains.push(val); // all vals presents
liContains.push(li); // all (sets of) LIs with the vals
}
else {
notContains.push(val); // all vals not present
}
});
alert(contains.length);
</script>