简体   繁体   English

使用javascript数组填充隐藏字段

[英]Populate hidden field with javascript array

I'm using django to submit a list of graphics/titles to a player in a table like this... 我正在使用django在这样的表中向播放器提交图形/标题列表...

<table>

    {% for title in titles %}

    <tr><form action="{% url 'show_title' %}" method="POST">
    {% csrf_token %}

    <td width="1%"><input type="image" src="{% static 'css/img/CG-Play.png' %}" border="0" alt="Submit" /></td>
    <td width="20%">{{title.name }}</td><td width="78%">{{title.occupation }}</td>
    <td><input type="checkbox" class="delete" name="delete" value="{{ title.id }}" /></td>
    <input type="hidden" name="f0" value="{{ title.name }}" />
    <input type="hidden" name="f1" value="{{ title.occupation }}" />
    </tr>
    </form>
    {% endfor %}

</table>

included in each form is a submit image/button that sends the correct info in the hidden fields to the player. 每个表单中都包含一个提交图像/按钮,用于将隐藏字段中的正确信息发送给播放器。 Also included is a checkbox with the class "delete" that I want to access in another form to delete the chosen/checked titles. 还包括一个带有“删除”类的复选框,我想以另一种形式访问该复选框以删除所选/选中的标题。

Due to the HTML layout, the form for delete is in a different location, so I want to use javascript to gather the "{{ title.id }}" data present in the form above, and send the data in a hidden field in form Nr. 由于HTML布局,用于删除的表单位于其他位置,因此我想使用javascript收集上面表单中显示的“ {{title.id}}”数据,并在以下位置的隐藏字段中发送数据Nr。 2 2

<form action="{% url 'delete_titles' %}" method="POST">
    {% csrf_token %}
    <input type="hidden" id="input_form_1" name="java_array" value="java_values" />
    <button type="submit" class="actionbutton deletebutton" name="delete_titles" onclick="return confirm('Delete (' + $(':checkbox:checked').length + ') title(s) - Are you sure?')" >Delete Titles</button>
 </form>

I tried with a script like this which is loaded in the header and almost does what I want, but it only updates the array when I refresh the page of course. 我尝试使用这样的脚本,该脚本加载在标题中,几乎可以完成我想要的操作,但是它仅在刷新页面时才更新数组。

<script type="text/javascript">
$(document).ready(function() {
    var java_values = [];
    $(".delete:checked").each(function(){
        java_values.push($(this).val());
    });
    $('#input_form_1').val(JSON.stringify(java_values));
    console.log(java_values);
});
</script>

How do I update the values in the array? 如何更新数组中的值?

You can store title.id as a session variable. 您可以将title.id存储为会话变量。

request.session['title_id'] = title.id

More info here: ( https://docs.djangoproject.com/en/2.0/topics/http/sessions/)[https://docs.djangoproject.com/en/2.0/topics/http/sessions/] 此处提供更多信息:( https://docs.djangoproject.com/en/2.0/topics/http/sessions/) [ https://docs.djangoproject.com/en/2.0/topics/http/sessions/]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM