javascript
  • jquery
  • forms
  • submit
  • 2013-10-08 153 views 0 likes 
    0

    我知道我必须添加返回false;在onsubmit中调用函数(我将它添加到函数中,并在HTML中添加onsubmit =“”)。但它不起作用......按Enter键或点击提交按钮后页面正在刷新。提交无法看到返回false;

    的JavaScript生成的表单:

    AnswersHTML += "<div><form onsubmit='cheking(this," + a + "," + b + ", this); return false;'><input class='put' type='text' size='40'><input type='submit' value='Проверить'></form> 
    

    和功能

    function checking(answer, nums, numq, what) { 
    var usr = answer; 
    if (isNaN(answer)) { 
        usr = answer.value; 
        if (answer.value.length == "") { 
         usr = "Вы ничего не ввели!" 
        } 
        else { 
         answer = answer.value.toLowerCase(); 
        } 
    } 
    if (answer == correct[nums][numq]) { 
        $(what).parent().parent().append("<br><span class='right'>Ответ: " + usr + "<br>Правильно!</span>").slideDown(); 
        $(what).parent().parent().find(".put").remove(); 
        $("#board").animate({ backgroundColor: '#2ecc71'}); 
        $("#board").animate({ backgroundColor: '#f1c40f'}, 1000); 
        if (isPhysics[nums][numq]) { 
         physics++; 
         $("#presult").text(physics); 
        } 
        else { 
         life++; 
         $("#lresult").text(life); 
        } 
    } 
    else { 
        $("#board").animate({ backgroundColor: '#e74c3c'}); 
        $("#board").animate({ backgroundColor: '#f1c40f'}, 1000); 
        var desciptionToWrong = "<br>" + description[nums][numq]; 
        if (description[nums][numq] == false) { 
         desciptionToWrong = "" 
        } 
        $(what).parent().parent().append("<br><span class='wrong'>Ответ: " + usr + "<br>Неправильно!" + desciptionToWrong + "</span>") 
        $(what).parent().parent().find(".put").remove(); 
        $(what).remove(".pressenter"); 
    } 
    return false; 
    

    }

    对不起,我可能是恶心的代码

    下面是完整的页面https://rawgithub.com/ruslankh/Kurchatovy/master/index.html

    功能问题,因为当我更换功能只是提醒,它很好

    +0

    尝试'返回检查(...);'而不是仅仅'检查(...)' – rps

    +0

    有几个剧本'checking'方法中的错误.. param'answer'指的是'form'元素而不是'input'元素...所以'answer.value'将是未定义的,并且'answer.value.length'会抛出一个错误 –

    +0

    @rps没有帮助( – Ruslan

    回答

    0

    它可能是由于类型。你在函数调用中有错误的拼写会导致错误,并且调用去服务器。

    变化

    'cheking(this," + a + "," + b + ", this); 
    

    'checking(this," + a + "," + b + ", this); 
    
    0

    你在线的onclick有一个错字,你离开了C'S的一出函数名。这个错误可能会终止点击处理程序,直到返回语句。

    0

    试试这个,

    AnswersHTML += "<div><form data-a='"+a+"' data-b='"+b+"' class='myform'><input class='put... " 
    

    SCRIPT

    $(function(){ 
        $(document).on('submit','form.myform',function(){ 
         checking(this, $(this).data('a'), $(this).data('b'), this); 
         return false; 
        }); 
    }); 
    
    相关问题