簡體   English   中英

如何在jquery.post調用中停止表單提交?

[英]How to Stop Form Submit on jquery.post call?

html

<form action="/jobseeker/profile/" method="post" id="langForm">     
    <input type="hidden" name="curform" value="langform">
    <div class="control-group">
        <label class="control-label">Language Name</label>
        <div class="controls">
            <input id="languageadd" maxlength="120" name="language" type="text" required/>
        </div>
    </div>
    <input class="btn btn-success" type="submit" value="save" />
</form>

jQuery腳本

$("#langForm").on("submit", function(event){    
    $.post('/jobseeker/profile/', $(this).serialize(),
    function(data){
        alert('AJAX successful');
        //CreateRow(jdata);
    }, "json"); 
    event.preventDefault(); 
});

/ jobseeker / profile /的views.py

def addlang(request):
    #curform=request.POST['curform']
    md=Languages()
    for i in request.POST.keys():
        if i=='curform':continue
        setattr(md,i,request.POST[i])
    md.save()
    n={ 
            "pk": md.pk,
            "lang":md.language,
            "read":md.read,
            "speak":md.speak,
            "write":md.write            
    }
    return HttpResponse(json.dumps(n), mimetype="application/json")

當我單擊“提交”按鈕時,防止默認設置不起作用,並且整個表單提交發生

提交按鈕后單擊

嘗試返回false;

 $("#langForm").on("submit", function(event){    
 $.post('/jobseeker/profile/', $(this).serialize(),
 function(data){
    alert('AJAX successful');
    //CreateRow(jdata);
 }, "json"); 
 return false;
});

嘗試刪除HTML表單中指定的action="/jobseeker/profile/"

嘗試這個

$("#langForm").submit(function(event){    
    $.post('/jobseeker/profile/', $(this).serialize(),
    function(data){

        /* stop form from submitting normally */
        event.preventDefault();

        alert('AJAX successful');
        //CreateRow(jdata);
    }, "json"); 
    event.preventDefault(); 
});

event.preventDefault(); 將停止表單的默認提交。

preventDefault()放在頂部,最后return false

$("#langForm").on("submit", function (event) {
    event.preventDefault();
    $.post('/jobseeker/profile/', $(this).serialize(),
        function (data) {
            alert('AJAX successful');
            //CreateRow(jdata);
        }, "json");
    return false;
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM