做一些计算在javascript onkeypress



我有一个HTML文本框在我的网页上显示当前问题的数量(一个小的网页来回答一些问题),我想让快捷方式的问题,用户想通过类型的问题数量在文本框。我使用下面的代码,但这不能正常工作。

例如,所有问题都是8,当我在TextBox中输入15并按enter时,if子句不起作用,Question变量设置为15。我使用警报函数来跟踪它,我知道if子句不能正确工作。有人能帮我检查一下吗?这是我所有的代码:

<?php
$All = 8;
$URL = "http://localhost/Test.php";
if(isset($_GET["edtQuestionNo"])){
    $QuestionNo = $_GET["edtQuestionNo"];
}else{
    $QuestionNo = 1;
}
?>
<html>
<head>
<title>Test Page</title>
<script type="text/javascript">
function KeyPress(e, URL, All){
    if(e.keyCode === 13){
        var Question = document.getElementsByName("edtQuestionNo")[0].value;
        if(Question > All){
            Question = All;
            alert(All + "  " + Question + "  yes");
        }
        else{
            alert(All + "  " + Question + "  no");
        }
        window.open(URL + "?edtQuestionNo=" + Question,"_self");
    }
}
</script>
</head>
<body>
    <form action="Test.php" method="get" name="FRMQuestion">
        <label>Enter question number : </label>
        <input type="text" name="edtQuestionNo" id="QuestionNo" value="<?php echo $QuestionNo; ?>" 
            onkeypress="KeyPress(event,'<?php echo $URL; ?>','<?php echo $All; ?>')">
        <br>
        <label>Question number is : <?php echo $QuestionNo; ?></label>
    </form>
</body>
</html>

我解决了1. 我必须使用parseInt函数来比较所有和问题值。因为它们属于不同的类型。2. 我把问题值(计算后)再次在HTML文本框,然后打开URL。我的代码是:

<?php
$All = 8;
$URL = "http://localhost/Test.php";
if(isset($_GET["edtQuestionNo"])){
    $QuestionNo = $_GET["edtQuestionNo"];
}else{
    $QuestionNo = 1;
}
?>
<html>
<head>
<title>Test Page</title>
<script type="text/javascript">
function KeyPress(e, URL, All){
    if(e.keyCode === 13){
        var Question = document.getElementsByName("edtQuestionNo")[0].value;
        if(parseInt(Question) > parseInt(All)){
            Question = All;
        }
        document.getElementsByName("edtQuestionNo")[0].value = Question;
        window.open(URL + "?edtQuestionNo=" + Question,"_self");
    }
}
</script>
</head>
<body>
    <form action="Test.php" method="get" name="FRMQuestion">
        <label>Enter question number : </label>
        <input type="text" name="edtQuestionNo" id="QuestionNo" value="<?php echo $QuestionNo; ?>" 
            onkeypress="KeyPress(event,'<?php echo $URL; ?>','<?php echo $All; ?>')">
        <br>
        <label>Question number is : <?php echo $QuestionNo; ?></label>
    </form>
</body>
</html>

最新更新