我正在尝试编写代码,这些代码将从元素中获取值并对唯一值进行排序,并最终显示在屏幕中。我的问题是我无法对唯一值进行排序。在我看来,问题出在数组中。感谢您的帮助,对于语法错误,我们深表歉意。
<pre id="a">
<span>D</span> <span>C</span> <span>E</span> <span>E</span> <span>D</span> </pre>
<form method="POST" id="form">
<input type="hidden" id="lan" value="" name="number">
<input type="submit" value="patvirtint">
</form>
<script type="text/javascript"> var M=document.getElementById('a');
// counts <span> elements
var N=M.getElementsByTagName('Span');
var largo = N.length;
document.getElementById("form").getElementsByTagName('input')[0].setAttribute("value", largo);
</script>
<?php
// Taking values from form to php code
if (!empty($_POST['number']) && isset($_POST['number'])) {
$counter = 0;
$band = $_POST['number'];
while ($counter<=$band-1) {
// looping values with javascript help
$labas1[]='"'.'<script type="text/javascript"> var M'.$counter.'=document.getElementById(\'a\');
var N'.$counter.'=M'.$counter.'.getElementsByTagName(\'Span\')['.$counter.'].innerHTML; document.write(N'.$counter.');</script>'.'",';
$counter++;
$result = array_unique($labas1);
}
print_r($result);
}
?>
最佳答案
您的数组$labas1
没有您的想法,请尝试以下操作:
<pre id="a">
<span>D</span> <span>C</span> <span>E</span> <span>E</span> <span>D</span>
</pre>
<form method="POST" id="form">
<input type="hidden" id="lan" value="" name="number">
<input type="submit" value="patvirtint">
<input type="hidden" id="myDATA" value="" name="myDATA"/>
</form>
<script type="text/javascript"> var M=document.getElementById('a');
// counts <span> elements
var N=M.getElementsByTagName('Span');
var largo = N.length;
var myData = Array();
for (var i = 0; i < N.length; i++) {
myData[i]=N[i].innerHTML;
}
document.getElementById("myDATA").value=myData;
document.getElementById("form").getElementsByTagName('input')[0].setAttribute("value", largo);
</script>
<?php
// Taking values from form to php code
if (!empty($_POST['number']) && isset($_POST['number'])) {
$counter = 0;
$band = $_POST['number'];
$my_labas1=$_POST['myDATA'];
echo $my_labas1;
$result=explode(",", $my_labas1);
$result = array_unique($result);
echo "<pre>";
var_dump($result);
echo "</pre>";
asort($result);
print_r($result);
}
?>
关于javascript - 谁将从<span>元素中获取值并对唯一值进行排序的代码,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32640582/