我有一个数组'名字'。我需要检查这个数组中的元素是否存在于html列表中。
php中的代码如下:
foreach ($name as $value2) {
echo "<script>
if($('li:not(:contains(\"".$value2."\"))'))
{
alert('".$value2." does not exists');
}
else
{
alert('".$value2." exists');
}
</script>";
}
但是我收到警报,因为每个元素都不存在值。
答案 0 :(得分:0)
您可以使用PHP数组:
<?php
$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)) {
echo "Got Irix";
}
if (in_array("mac", $os)) {
echo "Got mac";
}
?>
<强> reference 强>
您可以尝试这样:
<?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 } ?>
答案 1 :(得分:0)
假设您无法在服务器上测试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>