否则,如果JavaScript中的语句不能显示验证消息 [英] else if statement in javascript not able to display validation message

查看:79
本文介绍了否则,如果JavaScript中的语句不能显示验证消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如果你看下面的代码,如果如果符合语句,那么它显示的消息是正确的,但是当我确认符合语句并故意故障 else if 语句,而不是显示消息,它只显示一个空白。为什么它不显示< else的消息,如果在下面的javascript验证中满足语句:

 函数editvalidation(){
var isDataValid = true;
var currentAssesO = document.getElementById(currentAssessment);
var noStudentsO = document.getElementById(addtextarea);
var studentAssesMsgO = document.getElementById(studentAlert);

studentAssesMsgO.innerHTML =;

if(currentAssesO.value ==){
$('#targetdiv')。hide();
studentAssesMsgO.innerHTML =请从评估下拉菜单中选择要编辑的评估;
isDataValid = false;
} else if(noStudentsO.value ==){
$('#targetdiv')。hide();
studentAssesMsgO.innerHTML =你没有选择任何你想加入评估的学生;
isDataValid = false;
}
else {
studentAssesMsgO.innerHTML =;
}

return isDataValid;
}

更新: $ b

HTML:

选择框(选项添加到此框中):

 < select multiple =multiplename =addtextareaid =studentaddsize =10> 
< / select>

降下菜单:

 < select name =sessionid =sessionsDrop> 
< option value =>请选择< / option>
< option value ='20'> EWYGC - 10-01-2013 - 09:00< / option>
< option value ='22'> WDFRK - 11-01-2013 - 10:05< / option>
< option value ='23'> XJJVS - 12-01-2013 - 10:00< / option>
< / select> < / p为H.

ALERT MESSAGE:

 < div id ='studentAlert'>< / div> 

验证要求:


  • 如果下拉菜单为空,则显示评估需要从警报消息div中的下拉菜单中选择的消息。


  • 如果下拉菜单不为空,然后检查选择框是否包含任何选项,如果选择框不包含任何选项,则将div警报消息替换,表示没有选择学生添加到评估中。

  • >
  • 如果下拉菜单不为空并且选择框不为空(或者换句话说包含选项),那么div警报消息只是一个空字符串



解决方案



  function editvalidation(){
var isDataValid = true;
var currentAssesO = document.getElementById(currentAssessment);
var noStudentsO = document.getElementById(addtextarea);
var studentAssesMsgO = document.getElementById(studentAlert);
var errorMsg =;

studentAssesMsgO.innerHTML =;
if(currentAssesO.value ==|| noStudentsO.value ==){
$('#targetdiv')。hide();
isDataValid = false;

if(currentAssesO.value ==){
errorMsg + =请从评估下拉菜单中选择要编辑的评估;
}

if(noStudentsO.value ==){
errorMsg + =您没有选择任何想加入评估的学生;
}

studentAssesMsgO.innerHTML = errorMsg; //再加上消息的样式。
}

return isDataValid;






更新回答



请在< head> 部分中加入jQuery。

 < script src =http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js>< /脚本> 

更新您的脚本:

  function editvalidation()
{
if($(#sessionsDrop)。val()==)
$(#studentAlert)。 html(评估需要填写);
if($(#sessionsDrop)。val()!=&& $(#studentadd)。children()。length == 0)
$(# studentAlert)。html(没有学生被选中添加到评估中);
if($(#sessionsDrop)。val()!=&& $(#studentadd)。children()。length!= 0)
return true;
返回false;
}


I am having trouble displaying strings depending on the if/else statements in my validation.

If you look at the code below, if the if statement is met, then it displays the message which is fine, but then when I make sure the if statement is met and deliberately fail the else if statement, instead of displaying a message, it just displays a blank. Why is it not displaying a message for when else if statement is met in javascript validation below:

function editvalidation() {
     var isDataValid = true;
     var currentAssesO = document.getElementById("currentAssessment");
     var noStudentsO = document.getElementById("addtextarea");
     var studentAssesMsgO = document.getElementById("studentAlert");

     studentAssesMsgO.innerHTML = "";

     if (currentAssesO.value == ""){
         $('#targetdiv').hide();
         studentAssesMsgO.innerHTML = "Please Select an Assessment to edit from the Assessment Drop Down Menu";
         isDataValid = false; 
     }else if (noStudentsO.value == ""){
         $('#targetdiv').hide();
         studentAssesMsgO.innerHTML = "You have not Selected any Students you wish to Add into Assessment";
         isDataValid = false; 
     }
     else{
         studentAssesMsgO.innerHTML = ""; 
     }

     return isDataValid;
}

UPDATE:

HTML:

SELECT BOX (Options are appended into this box):

<select multiple="multiple" name="addtextarea" id="studentadd" size="10">
</select>

DROP DOWN MENU:

<select name="session" id="sessionsDrop">
<option value="">Please Select</option>
<option value='20'>EWYGC - 10-01-2013 - 09:00</option>
<option value='22'>WDFRK - 11-01-2013 - 10:05</option>
<option value='23'>XJJVS - 12-01-2013 - 10:00</option>
</select> </p> 

ALERT MESSAGE:

<div id='studentAlert'></div>

Reuirements for validation:

  • If drop down menu is empty, then display message that assessment needs to be select from drop down menu in alert message div.

  • If drop down menu is not empty, then check to see if the select box contains any options, if select box contains no options, then replace div alert message stating no students have been selected to add to assessment

  • If drop down menu is not empty and select box is not empty (or in other words contains an option), then div alert message is just an empty string ""

解决方案

Rephrase your JavaScript this way:

function editvalidation() {
    var isDataValid = true;
    var currentAssesO = document.getElementById("currentAssessment");
    var noStudentsO = document.getElementById("addtextarea");
    var studentAssesMsgO = document.getElementById("studentAlert");
    var errorMsg = "";

    studentAssesMsgO.innerHTML = "";
    if (currentAssesO.value == "" || noStudentsO.value == "") {
        $('#targetdiv').hide();
        isDataValid = false; 

        if (currentAssesO.value == "") {
            errorMsg += "Please Select an Assessment to edit from the Assessment Drop Down Menu";
        }

        if (noStudentsO.value == "") {
            errorMsg += "You have not Selected any Students you wish to Add into Assessment";
        }

        studentAssesMsgO.innerHTML = errorMsg; // Plus whatever styling for messages.
    }

    return isDataValid;
}


Updated answer

Please include jQuery by putting this in the <head> section.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

Update your script:

function editvalidation()
{
    if ($("#sessionsDrop").val()=="")
        $("#studentAlert").html("Assessment needs to be filled.");
    if ($("#sessionsDrop").val()!="" && $("#studentadd").children().length==0)
        $("#studentAlert").html("No students have been selected to add to assessment.");
    if ($("#sessionsDrop").val()!="" && $("#studentadd").children().length!=0)
        return true;
    return false;
}

这篇关于否则,如果JavaScript中的语句不能显示验证消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆