表单提交第一次失败,并在刷新页面时工作

Form submit fails the firstime and works upon refreshing the page

本文关键字:工作 刷新 第一次 失败 表单提交      更新时间:2023-09-26

我正在运行一个django web应用程序,当我第一次提交表单失败时,我需要刷新并再次提交表单以在其中发送数据,或者在web浏览器上进行反向转发。我正在使用一个javascript程序(editarea)在文本区域上自动着色代码

这是我的HTML文件:

<html>
<head>
    <title>Test</title>
    <script language="javascript" type="text/javascript" src="/static/edit_area/edit_area_full.js"></script>
    <script type="text/javascript">
        function load() {
            var combo = document.getElementById('selSeaShells1').value;
            editAreaLoader.init({
                id: "textarea_1"        // textarea id
                    , syntax: combo                // syntax to be uses for highgliting
                    , start_highlight: true     // to display with highlight mode on start-up
            });
        }
    </script>
</head>
<body onload="load();">
    <form id="codeid" method="post" enctype="application/x-www-form-urlencoded" name="code" action="DestinationAddress/function">
    <h3>Choose a language</h3>
    <select name="sellang1" id="selSeaShells1" onchange="load();">
        <option value="python">Python</option>
        <option value="perl">Perl</option>
        <option value="sql">SQL</option>
        <option value="cpp">C++</option>
        <option value="c">C</option>
        <option value="java">Java</option>
        <option value="css">Css</option>
    </select>
    <br>
    <textarea id="textarea_1" name="content" cols="80" rows="15" type="text"></textarea>
    <input id="thebutton" type="button" value="Submit" onclick="document.forms.codeid.submit();" />
    </form>
</body>
</html>

这里是视图.py:

def function(request):
encoded_data = urllib.urlencode(request.POST)
url=urllib2.urlopen('http://webappAddress:8000/function/?' + encoded_data)
tml= url.read()
return HttpResponse(tml)

您的javascript失败
var combo=document.getElementById('selSeaShells1').value;
Combo将是未定义的,因为您在页面加载完成之前调用了load()。
我会使用jquery并这样做:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> //download and get a local copy of jquery don't use googles.***<script>
$(document).ready(function() {
    load();
});
function load()
    {     
        var combo = $('#selSeaShells1 option:selected).val();
            editAreaLoader.init({
            id : "textarea_1"       // textarea id
            ,syntax: combo                // syntax to be uses for highgliting
            ,start_highlight: true      // to display with highlight mode on start-up
            });

    }
</script>

删除body标记上的onload="load();"
这将在页面加载后获得所选内容。