如何在警报中显示正确的号码 [英] How to display correct number in alert

查看:84
本文介绍了如何在警报中显示正确的号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到问题号码显示在警报中。它不显示问题编号,而是显示问题编号作为问题编号。因此,在警报验证中,例如:

 您在问题编号上有错误:115 

当它应该说您在问题编号上有错误:3 例如现在这个代码显示问题ID: value =<?php echo $ questionId?>



此代码显示问题编号: echo $ searchQuestionNo [array_search($ questionId,$ searchQuestionId)]



但是当我试图这样做时:

 < input type ='hidden'id ='num_groups'name ='num_groups'value ='<?php echo $ searchQuestionNo [array_search($ questionId,$ searchQuestionId)]`?>'> 

它不显示jquery alert的问题编号。说实话,发生的事情是验证不起作用,因为它没有出现。然而,如果我将上面的代码保存在问号id的哪个值中:

 < input type ='hidden' id ='num_groups'name ='num_groups'value ='<?php echo $ questionId?>'> 

然后它在alert中显示questionId,并且警报实际上起作用。

所以我的问题是如何在警报中包含问题编号值,但是还能够使验证工作,就像问题提交一样?



以下是主代码,您可以看到所有内容来自哪里:

PHP:

 <? 

//这将保存搜索结果
$ searchQuestionId = array();
$ searchQuestionNo = array();

//将结果提取到一个数组

//获得结果并赋值变量(带db的前缀)
$ stmt-> bind_result($ dbSessionId, $ dbSessionName,$ dbQuestionId,$ dbQuestionNo,$ dbQuestionContent,$ dbAnswer,$ dbAnswerId,$ dbQuestionMarks,$ dbOptionId,$ dbOptionType);


$ searchQuestionId [] = $ dbQuestionId;
$ searchQuestionNo [] = $ dbQuestionNo;
}

?>



< / head>

< body>


< form id =PenaltyMarksaction =<?php echo htmlentities($ action);?>方法= POST >


<?php

$ ques_ans = array(); //针对问题存储不正确的答案。

$ q_occ_count = array_count_values($ searchQuestionId);
foreach($ searchQuestionId as $ key => $ questionId)
{
.....
}


?> gt ;
< table id ='penaltytbl'>
< thead>
< tr>
< / tr>
< / thead>
< tbody>
<?php

foreach($ ques_ans as $ questionId => $ inc_ans)
{


?>

< tr class =questiontd>

< td class =questionnumtd q<?php echo $ searchQuestionNo [array_search($ questionId,$ searchQuestionId)]?> _qnumrowspan =<?php echo $ q_row_span?> ><?php echo $ searchQuestionNo [array_search($ questionId,$ searchQuestionId)]?>
< input type =hiddenname =numQuestionvalue =<?php echo $ questionId?> />
< / td>


< td class =answermarkstd>
< input class =individualMarks q<?php echo $ questionId?> _markq_group =1name =answerMarks []type =textdata-type =qmarkdata-qnum =<?php echo $ questionId?> onkeypress =return isNumberKey(event)maxlength =3value =0/>
< / td>

< / tr>
<?php
}

?>
< / tbody>
< / table>

< p>
< input type ='hidden'id ='num_groups'name ='num_groups'value ='<?php echo $ questionId?>'>
< input id =submitBtnname =submitPenaltytype =submitvalue =Submit Marks/>
< / p>

< / form>

Jquery:

 < script type =text / javascript> 


myClickHandler = function(e){
var ng = $('#num_groups')。val(); (var group = 1; group
<= ng; group ++){
if(!validation(group))return false;
}

});


函数验证(组){
var msg = [];

var nb = 0; //空白值的数目
$(input [data-qnum ='+ group +')。each(function(){
if($(this).val() ==''){
nb ++;
return false;
}
});
if(nb!= 0){
msg.push(\\\•您没有在所有Penalty Marks文本框中输入值\\\
);
}

if(msg.length>
0){
alert(您在问题编号上有错误:+ group +\ n \\ \\ n+ msg.join(\\\
));
返回false;
} else {
return true;
}
}




< /脚本>


解决方案

回复问题编号:
$ b $ pre $ echo $ searchQuestionNo [array_search($ questionId,$ searchQuestionId)]




将您的问题编号添加为属性 q_number 您的输入元素:

 < input class =individualMarks q<?php echo $ questionId?> _markq_number =<?php echo $ searchQuestionNo [array_search($ questionId,$ searchQuestionId)];?> q_group =1name =answerMarks []type =textdata-type =qmarkdata-qnum =<?php echo $ questionId?> onkeypress =return isNumberKey(event)maxlength =3value =0/> 

将它用于验证函数

 函数验证(组){
var msg = [];

var qNumber = null;

var nb = 0; //空白值的数量
$(input [data-qnum ='+ group +')。each(function(){

//分配问题编号
qNumber = $(this).attr('q_number');

if($(this).val()==''){
nb ++;
返回false;
}
});
if(nb!= 0){
msg.push(\\\•您没有在所有Penalty Marks文本框中输入值\\\
);
}

//使用qNumber而不是组变量
if(msg.length> 0){
alert(您在问题编号: + qNumber +\\\
\\\
+ msg.join(\\\
));
返回false;
} else {
return true;
}
}


I am having issue with a question number displayed in an alert. Instead of displaying the question number, it is displaying the question id as the question number. So in the alert validation it states for example:

You have errors on question number: 115

When it should say You have errors on question number: 3 for example

Now this code display question id: value="<?php echo$questionId?>"

This code display question number: echo$searchQuestionNo[array_search($questionId, $searchQuestionId)]

But when I tried to do this:

<input type='hidden' id='num_groups' name='num_groups' value='<?php echo$searchQuestionNo[array_search($questionId, $searchQuestionId)]`?>'>

It does not display question number for jquery alert. To be honest what happens is that the validation doesn't work as it doesn't appear. Yet if I kept the above code to what it was which value was the question id:

<input type='hidden' id='num_groups' name='num_groups' value='<?php echo$questionId?>'>

Then it displays the questionId in the alert and the alert actually works.

So my question is how do I include the question number value in the alert yet be able to get the validation to work as well, like it does for questionid?

Below is main code so you can see where everything comes from:

PHP:

    <?

       // This will hold the search results
    $searchQuestionId = array();
    $searchQuestionNo = array();

    // Fetch the results into an array

   // get result and assign variables (prefix with db)
$stmt->bind_result($dbSessionId, $dbSessionName, $dbQuestionId, $dbQuestionNo, $dbQuestionContent, $dbAnswer, $dbAnswerId, $dbQuestionMarks, $dbOptionId, $dbOptionType);


        $searchQuestionId[] = $dbQuestionId;
        $searchQuestionNo[] = $dbQuestionNo;
      } 

?>



</head>

<body>


<form id="PenaltyMarks" action="<?php echo htmlentities($action); ?>" method="post">


<?php

$ques_ans = array();    //to store incorrect answers against ques no.

$q_occ_count = array_count_values($searchQuestionId);
foreach($searchQuestionId as $key => $questionId)
{
.....
}


?>
<table id='penaltytbl'>
<thead>
<tr>
<th class='questionth'>Question No.</th>
<th class='questionth'>Question</th>
</tr>
</thead>
<tbody>
<?php

foreach($ques_ans as $questionId => $inc_ans)
{


?>

<tr class="questiontd">

<td class="questionnumtd q<?php echo$searchQuestionNo[array_search($questionId, $searchQuestionId)]?>_qnum" rowspan="<?php echo$q_row_span?>"><?php echo$searchQuestionNo[array_search($questionId, $searchQuestionId)]?>
<input type="hidden" name="numQuestion" value="<?php echo$questionId?>" />
    </td>


<td class="answermarkstd">
<input class="individualMarks q<?php echo$questionId?>_mark"  q_group="1" name="answerMarks[]" type="text" data-type="qmark" data-qnum="<?php echo$questionId?>" onkeypress="return isNumberKey(event)" maxlength="3" value="0" />
</td>

</tr>
<?php
}

?>
</tbody>
</table>

<p>
<input type='hidden' id='num_groups' name='num_groups' value='<?php echo$questionId?>'>
<input id="submitBtn" name="submitPenalty" type="submit" value="Submit Marks" />
</p>

</form>

Jquery:

  < script type = "text/javascript" >


    myClickHandler = function (e) {
      var ng = $('#num_groups').val();
      for (var group = 1; group 
           <= ng; group++) {
        if (!validation(group)) return false;
      }

    });


    function validation(group) {
      var msg = [];

      var nb = 0; // Number of blank values
      $("input[data-qnum='" + group + "']").each(function () {
        if ($(this).val() == '') {
          nb++;
          return false;
        }
      });
      if (nb != 0) {
        msg.push("\u2022 You have not entered in a value in all the Penalty Marks textbox \n");
      }

      if (msg.length >
          0) {
        alert("You have errors on Question Number: " + group + "\n\n" + msg.join("\n"));
        return false;
      } else {
        return true;
      }
    }




    < /script>

解决方案

I assume that, as you said, this code permits to retieve the question number :

echo$searchQuestionNo[array_search($questionId, $searchQuestionId)]


Add your question number as an attribute q_number in your input element :

<input class="individualMarks q<?php echo$questionId?>_mark" q_number="<?php echo $searchQuestionNo[array_search($questionId, $searchQuestionId)]; ?>" q_group="1" name="answerMarks[]" type="text" data-type="qmark" data-qnum="<?php echo$questionId?>" onkeypress="return isNumberKey(event)" maxlength="3" value="0" />

And use it in your validation function

function validation(group) {
  var msg = [];

  var qNumber = null;

  var nb = 0; // Number of blank values
  $("input[data-qnum='" + group + "']").each(function () {

    //Assign the question number
    qNumber = $(this).attr('q_number');

    if ($(this).val() == '') {
      nb++;
      return false;
    }
  });
  if (nb != 0) {
    msg.push("\u2022 You have not entered in a value in all the Penalty Marks textbox \n");
  }

  //Use qNumber instead of the group variable
  if (msg.length > 0) {
    alert("You have errors on Question Number: " + qNumber + "\n\n" + msg.join("\n"));
    return false;
  } else {
    return true;
  }
}

这篇关于如何在警报中显示正确的号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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