我是新手,我正在try 使用它在提交表单之前对其进行验证.第一个函数"valiateYouTube"可以工作并成功地判断YouTube代码是否已经输入,但第二个函数"valiateSongandTitle"出现了问题.
为了使第二个函数valiateSongandTitle有效,我满足以下条件:
- 如果用户没有 Select 一首歌曲(比如他们将下拉列表保留为"No song selected"),那么他们需要输入一个"title"才能使其有效.
这是我得到的:
<form action="add-video.php" method="post" onsubmit="return validateForm(event)" id="AddVid">
<select name="songid">
<option value="">No song selected</option>
<option value="1">Dr Jones</option>
<option value="2">Spaceship</option>
<option value="3">Fantasy</option>
</select>
<input type="text" id="ytcode" name="ytcode" />
<div id="formyoutubecode"></div>
<input type="text" name="title" size="50" />
<div id="formtitle"></div>
<script>
function validateForm(event) {
event.preventDefault();
var YouTubeValid = validateYouTube();
var SongandTitleValid = validateSongandTitle();
if (!YouTubeValid || !SongandTitleValid) {
return;
}
event.target.submit();
}
function validateYouTube() {
var a = document.forms["AddVid"]["ytcode"].value;
if (a == null || a == "") {
document.getElementById('formyoutubecode').innerHTML = "<small><font color='red'>YouTube Code Required</font></small>";
return false;
}
return true;
}
function validateSongandTitle() {
var b = document.forms["AddVid"]["songid"].value;
var c = document.forms["AddVid"]["title"].value;
if ((b == "No song selected" && c == "")) {
document.getElementById('formtitle').innerHTML = "<small><font color='red'>Video title *Required</font></small>";
return false;
}
return true;
}
</script>
请告诉我为什么它不起作用?先谢谢你.