对不起,如果我的标题令人困惑,我不知道如何准确地描述我正在使用的数组。我使用简单的ajax,以便用户可以选择案例上的提供者数量。然后填充正确数量的下拉框。下拉菜单是用php/html代码创建的其中q是用户选择的提供商数量
for ($i=1; $i<=(intval($q)); $i++)
{
echo "Name of Anesthesia Provider " . $i . ": ";
echo "<select name='"anesthesiaprovider[]'">";
$result = mysqli_query($con,"SELECT * FROM Anesthesia_Provider ORDER BY Lname");
if (!(mysqli_query($con, $sql))) {
echo "<br>";
echo 'Error in inserting data: ' . mysqli_error($con);
}
echo "<option name='"anesthesiaprovider[]'" value='"selectprovider'">Anesthesia Provider " . $i . "</option>";
while($row = mysqli_fetch_array($result))
{
echo "<option name='"reviewer'" value='"" . $row['Anesthesia_Provider_Number'] . "'">" . $row['Lname'] . ", " . $row['Fname'] . "</option>";
}
echo "</select>";
echo "<br>";
echo "<br>";
}
我想使用javascript来确保为每个框选择一个提供商。如果没有选中,则用黄色突出显示。这是我的javascript代码,它验证了表单onsubmit:
function validateQualityReport(theForm)
{
var reason = "";
reason += validateCase(theForm.case);
reason += validateAnesthesiologists(theForm.element['anesthesiaprovider[]'])
if (reason != "") {
alert("Some fields need correction:'n" + reason);
return false;
}
alert("All fields are filled correctly");
return false;
}
function validateAnesthesiologists(fld){
var error = "";
alert(fld.length);
for (var i = 0; i < fld.length; i++) {
if (fld[i].value =="selectprovider"){
fld[i].style.background = 'Yellow';
error += "Make a selection for provider " + (i+1).toString() + "'n";
}else{
fld[i].style.background = 'White';
}
}
return error;
}
该代码适用于所有情况,除了只选择了一个提供程序。然后它说那个领域。长度是56(所以下拉框中的所有内容),当它高亮时,它不会高亮整个下拉框,它只高亮第一个。这是怎么回事?提前感谢你的帮助。
你的问题有点令人困惑,但据我所知,唯一的第一个元素被"突出显示"为黄色,因为你是专门为它过滤的:if (fld[i].value =="selectprovider"){ fld[i].style.background = 'Yellow';
如果将else语句从fld[i].style.background = 'White';
来fld[i].style.background = 'Yellow';
列表中剩余的元素也会被"高亮显示"。
对于它的价值,我个人也会通过使用node.children.length
和node.children[i]
您的php代码需要进行以下更改
// move sql query out of loop as don't need to read 'q' times
$result = mysqli_query($con,"SELECT * FROM Anesthesia_Provider ORDER BY Lname");
if (!(mysqli_query($con, $sql))) {
echo '<br>Error in inserting data: ' . mysqli_error($con);
} else {
for ($i=1; $i<=(intval($q)); $i++)
{
echo "Name of Anesthesia Provider " . $i . ": ";
echo "<select name='"anesthesiaprovider[]'">";
// remove name attribute as invalid HTML
echo "<option value='"selectprovider'">Anesthesia Provider " . $i . "</option>";
// move to start of result - untested but should work
$result->data_seek(0);
// loop through results
while($row = mysqli_fetch_array($result))
{
echo "<option value='"" . $row['Anesthesia_Provider_Number'] . "'">" . $row['Lname'] . ", " . $row['Fname'] . "</option>";
}
echo "</select>";
echo "<br>";
echo "<br>";
}}
你的JavaScript代码现在只能找到与每个
我不知道为什么你得到数字56如果只有一个列表框显示。