简体   繁体   English

将输出从javascript发送到Django中的html DIV

[英]Send Output from javascript to html DIV in Django

I have an HTML page in Django where I send my input from the form to javascript and which will inturn will send it to the views, get processed and the output will come to javascript. 我在Django中有一个HTML页面,我将表单中的输入发送到javascript,然后将其发送到视图,进行处理,输出将转到javascript。 Now I want to display this output back in the same html page in one particular DIV. 现在我想在一个特定的DIV中的同一个html页面中显示此输出。

My form is 我的表格是

<form id="form_id" method="POST" action="/your_Value/">
    {% csrf_token %}
    <label for="your_Value">Sentence: </label>
    <input id="your_Value" type="text" name="your_Value">
    <input type="submit" value="Compare">
</form>
<div class="details"></div>

My javascript is 我的javascript是

<script type="text/javascript">
    $(function() {
        $(".details").hide();
        $("#form_id").submit(function(event) {
            event.preventDefault();
            var friendForm = $(this);
            var posting = $.post(friendForm.attr('action'), friendForm.serialize());
            posting.done(function(data) {
                $(".details").show();
            });
            posting.fail(function(data) {
                alert("Fail")
            });
        });
    });
</script>

Where the values from posting.done() should go to 'details' div. 来自posting.done()的值应该转到'details'div。

Kindly let me know how to send the values from javascript to HTML DIV and display it. 请告诉我如何将值从javascript发送到HTML DIV并显示它。

Thanks in advance!! 提前致谢!!

include a pointer for the output. 包括输出的指针。

posting.done(function(data) {
  $(".details").show();  
  $(".details").html(data);
});

reference: https://www.w3schools.com/jquery/html_html.asp 参考: https//www.w3schools.com/jquery/html_html.asp

将 javascript output 传递给 html<div> 作为参数</div><div id="text_translate"><p>我有一个 m_template.html 文件,其脚本如下:</p><pre> &lt;script&gt; $("#id_country").change(function () { var countryId = $(this).val(); // get the selected country ID from the HTML input $.ajax({ // initialize an AJAX request url: '/ajax/ajax_load_cities', data: { 'countries': countryId // add the country id to the GET parameters }, dataType: 'json', success: function (data) { // here data comes from url output or load_cities function $("#preview-items").html(data.tags); } }); }); &lt;/script&gt;</pre><p> 在同一个模板中。html 我定义了如下部分:</p><pre> &lt;div id="preview-items"&gt; {% for item in itemslist %} &lt;label class="btn btn-primary mr-1"&gt; &lt;input type="checkbox" id="checklist" value="{{ item.0 }}"&gt; &lt;span&gt; {{ item.1 }} &lt;/span&gt; &lt;/label&gt; {% endfor %} &lt;/div&gt;</pre><p> 和 load_cieties:</p><pre> def load_cities(request): .... data = { 'tags': list(cities) } return JsonResponse(data)</pre><p> <strong>问题发生的地方:</strong></p><p> 问题出在$("#preview-items").html(data.tags); 它不是传递列表,而是替换&lt;div&gt;中的列表,所以目前我有类似的东西:</p><blockquote><p> id1name1,id2name2,id3name3,...</p></blockquote><p> <strong>我的问题:</strong></p><p> 如何将作为元组列表的data.tags作为参数传递给部分,在该参数中我可以按照我已经定义的方式设置列表项的样式?</p><p> data.tags类似于[(id1, name1), (id2, name2), (id3, name3), ..]</p></div> - Pass javascript output to a html <div> as a parameter

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HTML中的JavaScript div输出不起作用 - Javascript div output in html not working 从Javascript输出HTML - Output html from Javascript 隐藏的Div没有使用javascript输出 - No output from hidden Div with javascript 将文本从HTML div发送到HTML textarea - Send text from a HTML div to a HTML textarea 如何输出HTML&#39; <div> &#39;元素和Javascript - How to output HTML '<div>' element with Javascript 如何将JavaScript变量“输出”到HTML div中 - How to 'output' a JavaScript variable into an HTML div 将 javascript output 传递给 html<div> 作为参数</div><div id="text_translate"><p>我有一个 m_template.html 文件,其脚本如下:</p><pre> &lt;script&gt; $("#id_country").change(function () { var countryId = $(this).val(); // get the selected country ID from the HTML input $.ajax({ // initialize an AJAX request url: '/ajax/ajax_load_cities', data: { 'countries': countryId // add the country id to the GET parameters }, dataType: 'json', success: function (data) { // here data comes from url output or load_cities function $("#preview-items").html(data.tags); } }); }); &lt;/script&gt;</pre><p> 在同一个模板中。html 我定义了如下部分:</p><pre> &lt;div id="preview-items"&gt; {% for item in itemslist %} &lt;label class="btn btn-primary mr-1"&gt; &lt;input type="checkbox" id="checklist" value="{{ item.0 }}"&gt; &lt;span&gt; {{ item.1 }} &lt;/span&gt; &lt;/label&gt; {% endfor %} &lt;/div&gt;</pre><p> 和 load_cieties:</p><pre> def load_cities(request): .... data = { 'tags': list(cities) } return JsonResponse(data)</pre><p> <strong>问题发生的地方:</strong></p><p> 问题出在$("#preview-items").html(data.tags); 它不是传递列表,而是替换&lt;div&gt;中的列表,所以目前我有类似的东西:</p><blockquote><p> id1name1,id2name2,id3name3,...</p></blockquote><p> <strong>我的问题:</strong></p><p> 如何将作为元组列表的data.tags作为参数传递给部分,在该参数中我可以按照我已经定义的方式设置列表项的样式?</p><p> data.tags类似于[(id1, name1), (id2, name2), (id3, name3), ..]</p></div> - Pass javascript output to a html <div> as a parameter 如何将HTML字符串发送到Django模板JavaScript? - How to send the html string to django template javascript? 将输出从javascript重定向到HTML - Redirect output from javascript to HTML 来自javascript的错误html输出 - wrong html output from the javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM