【发布时间】:2018-09-21 09:48:59
【问题描述】:
我尝试了以下方式,但请求就像
[21/Sep/2018 14:48:45] "GET /buildknowledge/sharing?bid=75&sharewith=16,17 HTTP/1.1" 500 14382
要求:
[21/Sep/2018 14:48:45] "GET /buildknowledge/sharing?bid=75&sharewith=16&sharewith=17 HTTP/1.1" 500 14382
我尝试了以下方法:
表格:
<div class="modal-body">
<p class="statusMsg"></p>
<form role="form">{% csrf_token %}
<div class="form-check">
<label for="sharewith">Share with</label></br>
{% for sharewith in sharewithonly %}
<input class="form-check-input position-static" name="mycheckboxes[]" id="myCheckboxes" type="checkbox" value="{{ sharewith.id }}">
<label>{{ sharewith.email }}</label></br>
{% endfor%}
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary submitBtn" onclick="shareForm()">SUBMIT</button>
</div>
</div>
</div>
Ajax 和 js 部分:
function shareForm() {
console.log(node_name);
var token = '{{csrf_token}}';
var sharewith =getChecked();
function getChecked(){
var items=document.getElementsByName('mycheckboxes[]');
var selectedItems=new Array();
for(var i=0; i<items.length; i++)
{
if(items[i].type=='checkbox' && items[i].checked==true)
selectedItems.push(items[i].value);
}
return(selectedItems);
};
$.ajax({
headers: {"X-CSRFToken": token},
type: 'GET',
url: 'sharing',
dataType: "json",
traditional: true,
data: 'sharewith=' + sharewith ,
beforeSend: function () {
$('.submitBtn').attr("disabled", "disabled");
$('.modal-body').css('opacity', '.5');
},
success: function (msg) {
if (msg == 'ok') {
$('#inputName').val('');
}
});
}
}
我只尝试了 POST 请求,只是为了清楚地解释我在这里使用了 GET。 需要如何分配复选框值
【问题讨论】:
-
<form role="form">— 这是一种形式,而不是伪装成一种形式的东西,role="form"毫无意义。
标签: javascript ajax forms