[英]validate drop down list echoed from php to html with javascript?
好的,這個問題的標題可能有些混亂,我將嘗試向您解釋:我有一個php文件,其中的數組包含多個表單字段,如下所示:
$var = array(0 => '<input type=....>', 1=> '<input type=....>');
我創建了一個while循環來遍歷數組以在表中顯示它們,如下所示:
echo '<table>';
while($row = mysql_fetch_array($result)){
$num = $row['fieldId'];
$field = $num.' '.$row['field'];
echo '<tr><td>'.$field.'</td>';
echo '<td>'.$var[$q].'</td></tr>';
echo '<tr><td> </td></tr>';
$q++;
}; echo '</table>';
我將這個php文件包含在html文件中,因此我可以得到一個包含兩列的表,第一列包含來自數據庫的$ field,第二列包含$ var [$ q],這是從$ var數組中獲取的一些輸入字段。 相信與否,這實際上是可行的,現在的問題是,在$ var數組中,我有一個帶有多個選項的<select>
字段,我想用一些JavaScript來驗證該字段,但是由於某些原因,我無法獲取該值在<option value="SOMEVALUE">
分配。 我不知道為什么,但我不能,有一件事我發現奇怪的是,在$ VAR陣列我有我可以用JavaScript驗證一些單選按鈕。
更多信息:包含php文件的html文件具有以下功能:
<html>....
<form name="mainForm">
<?php
include 'myPhpFile.php';
?>
<input type="button" name="btnNext" onclick="functionToValidate();">
</form>
...</html>
所以基本上,當我包括我的php文件時,它會創建一個表並向其中添加字段,這些字段使用mysql數據庫中的數據以及使用$ var數組顯示的所有輸入字段填充,上面顯示的所有方法都有效,但是問題是當我單擊下一步按鈕它調用了一個javascript函數來驗證字段,一切正常,直到到達<select>
輸入為止,我可以驗證單選按鈕,文本字段和textareas,但我不能驗證那些<select>
這是我正在使用的javascript函數:
function myFunctionNameHere(){
if(!document.mainForm.p1_1[0].checked && !document.mainForm.p1_1[1].checked)
alert("1 - some warning here");
else if(!document.mainForm.p1_2[0].checked && !document.mainForm.p1_2[1].checked)
alert("2 - some warning here");
else if(!document.mainForm.p1_3[0].checked && !document.mainForm.p1_3[1].checked)
alert("3 - some warning here");
else if(!document.mainForm.p1_4[0].checked && !document.mainForm.p1_4[1].checked)
alert("4 - some warning here");
else if(!document.mainForm.p1_5[0].checked && !document.mainForm.p1_5[1].checked)
alert("5 - some warning here");
else if(!document.mainForm.p1_6[0].checked && !document.mainForm.p1_6[1].checked)
alert("6 - some warning here");
else if(document.mainForm.p1_7.value == "")
alert("7 - some warning here");
// this condition does not work:
else if(document.mainForm.p1_8.selectedIndex == 0)
alert("8 - some warning here);
// can't get the selectd field from the html select field
else
window.location = "OTHERPAGE.html";
}
這幾乎就是函數的功能,所有條件都可以正常工作,但我要加上注釋。 (我仍在學習,所以請不要嘲笑我的代碼……請。)
對不起,我實際上解決了這個問題,我的php數組中的html代碼中有一些錯誤。 現在它實際上可以工作了。 謝謝。
這是您在javascript中訪問選擇值的方式:
<form name="mainform">
<select name="dropbox_select">
<option value="value0">option0</option>
<option value="value1">option1</option>
<option value="value2">option2</option>
</select>
</form>
document.forms.mainform.dropbox_select.options[0].value
document.forms.mainform.dropbox_select.options[1].value
document.forms.mainform.dropbox_select.options[2].value
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.