Вопрос по jquery, javascript, ajax – Что значит «вернуть ложь»?

18

Я написал веб-страницу, где пользователь может ввести запись в журнале, которая хранится в базе данных, а затем извлекается и распечатывается на странице, используяajax, Я все еще новичок вajax и мне было интересно, если кто-нибудь может объяснить мне, чтоreturn false; сделать в конце моего кода? и нужно ли это вообще?

Если я поставлю второй код Ajax послеreturn false код не работает! не могли бы вы объяснить мне, почему?

//handles submitting the form without reloading page 
$('#FormSubmit').submit(function(e) {
    //stores the input of today's data
    var log_entry = $("#LogEntry").val();
    // prevent the form from submitting normally
    e.preventDefault();

    $.ajax({
        type: 'POST',
        url: 'behind_curtains.php',
        data: {
            logentry: log_entry
        },
        success: function() {
            alert(log_entry);
            //clears textbox after submission
            $('#LogEntry').val("");
            //presents successs text and then fades it out
            $("#entered-log-success").html("Your Entry has been entered.");
            $("#entered-log-success").show().fadeOut(3000);
        }
    });
    //prints new log entries on page upon submittion
    $.ajax({
        type: 'POST',
        url: '/wp-content/themes/childOfFanwood/traininglog_behind_curtains.php',
        data: {
            log_entries_loop: "true"
        },
        success: function(data) {
            alert(data);
            $("#log-entry-container").html("");
            $("#log-entry-container").html(data);
        }
    });
    return false;
});
​
Error: User Rate Limit Exceeded gdoron
Error: User Rate Limit Exceeded user557846

Ваш Ответ

5   ответов
0

Error: User Rate Limit Exceeded

2

Error: User Rate Limit Exceededreturn false;Error: User Rate Limit Exceeded

Error: User Rate Limit Exceedede.preventDefault();

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
36

What I'll write here is true for jQuery events,
For vanilla javascript events read @T.J. Crowder comment at the bottom of the answer


return falseError: User Rate Limit ExceededsubmitError: User Rate Limit Exceeded

return falseError: User Rate Limit Exceeded

return false is equivalent to event.preventDefault() + event.stopPropagation()

Error: User Rate Limit Exceededreturn xxxError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded


Error: User Rate Limit Exceeded"real"Error: User Rate Limit Exceededreturn falseError: User Rate Limit Exceededevent.preventDefault():

Markup:

<div id="theDiv">
    <form id="theForm" >
        <input type="submit" value="submit"/> 
    </form>
</div>​

JavaScript:

$('#theDiv').submit(function() {
    alert('DIV!');
});
$('#theForm').submit(function(e) {
    alert('FORM!');
    e.preventDefault();
});​

Error: User Rate Limit ExceededpreventDefault()Error: User Rate Limit Exceeded

Live DEMO

Error: User Rate Limit Exceededreturn false:

$('#theDiv').submit(function() {
    alert('DIV!');
});
$('#theForm').submit(function(event) {
    alert('FORM!');
    return false;   
    // Or:
    event.preventDefault(); 
    event.stopPropagation();
});​

Error: User Rate Limit Exceeded

Live DEMO


What does return false do in vanilla javascript events

return false from a DOM2 handler (addEventListener) does nothing at all (neither prevents the default nor stops bubbling; from a Microsoft DOM2-ish handler (attachEvent), it prevents the default but not bubbling; from a DOM0 handler (onclick="return ..."), it prevents the default (provided you include the return in the attribute) but not bubbling; from a jQuery event handler, it does both, because that's a jQuery thing. Details and live tests here – T.J. Crowder

Error: User Rate Limit ExceededstopPropagationError: User Rate Limit Exceeded
Error: User Rate Limit Exceededstackoverflow.com/questions/1357118/….
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceedede.preventDefault();Error: User Rate Limit Exceededreturn false;Error: User Rate Limit Exceeded
3

Error: User Rate Limit ExceededreturnError: User Rate Limit ExceededfalseError: User Rate Limit ExceededfalseError: User Rate Limit Exceeded

0

Error: User Rate Limit Exceeded

Похожие вопросы