如何将表单中的多个复选框字段分配给ajax请求

时间:2018-09-11 06:25:41

标签: javascript ajax django forms

我尝试了以下方法,但没有工作,我从表单中获得名称,但未分配任何内容。 我希望所有选中的复选框都分配给一个id“ sharewith”,并且应该将其与ajax请求一起发送到服务器

表格:

 <div class="modal-body">
            <p class="statusMsg"></p>
            <form role="form">{% csrf_token %}
                <div class="form-group">
                    <label for="inputName">kcategory</label>
                    <input type="number" class="form-control" id="inputName" placeholder=node name/>
                </div>
                <div class="form-check" id="sharewith">
                    <label for="sharewith">Share with</label></br>
                 {% for sharewith in users %}
                   <input class="form-check-input position-static" type="checkbox"  value="{{ sharewith.uid }}">
                     <label>{{ sharewith.umail }}</label></br>
                  {% endfor%}
                </div>
            </form>
        </div>

ajax:

function submitContactForm() {
   var token = '{{csrf_token}}';
   var name = $('#inputName').val();
var sharewith = $("#sharewith").val()
if (name.trim() == '') {
    alert('Please enter your name.');
    $('#inputName').focus();
    return false;
}else{
    $.ajax({
        headers: { "X-CSRFToken": token },
        type:'POST',
        url:'sharing',
        dataType:"json",
        traditional: true,
        data:'contactFrmSubmit=1&name='+name+'&sharewith'+sharewith,
        beforeSend: function () {
            $('.submitBtn').attr("disabled","disabled");
            $('.modal-body').css('opacity', '.5');
        },
        success:function(msg) {
            if (msg == 'ok') {
                $('#inputName').val('');
                $('.statusMsg').html('<span style="color:green;">sucessfully saved</p>');
            } else {
                $('.statusMsg').html('<span style="color:red;">Some problem occurred, please try again.</span>');
            }
            $('.submitBtn').removeAttr("disabled");
            $('.modal-body').css('opacity', '');
        }

    });
}

}

我该如何分配所有复选框以与任何人共享

1 个答案:

答案 0 :(得分:0)

在模板中输入的复选框的输入类中添加名称和ID

<input class="form-check-input position-static" name="mycheckboxes" id="myCheckboxes" type="checkbox"  value="{{ sharewith.id }}">

ajax和JS:

function SubmitContactForm() {

    var token = '{{csrf_token}}';
    var name = document.getElementById("inputName").placeholder;

    var sharewith =getChecked();
    function getChecked(){
        var inputs = document.getElementsByName('mycheckboxes');
        var key_values = [];
      for(i=0; i<inputs.length; i++) {
         input = inputs[i];
      if (input.type=='checkbox' && input.checked) {
        key_values.push(
            encodeURIComponent(input.name) +
            "=" +
            encodeURIComponent(input.value)
        );
    }
}
var form_encoded_data = key_values.join("&");
return form_encoded_data;
};


    if (name.trim() == '') {
        alert('select any node to share.');
        $('#inputName').focus();
        return false;
    } else {
        $.ajax({
            headers: {"X-CSRFToken": token},
            type: 'POST',
            url: 'sharing',
            dataType: "json",
            traditional: true,
            data: 'name='+ name+"&"+sharewith ,
            beforeSend: function () {
                $('.submitBtn').attr("disabled", "disabled");
                $('.modal-body').css('opacity', '.5');
            },
            success: function (msg) {
                ##copy and paste
                }
                $('.submitBtn').removeAttr("disabled");
                $('.modal-body').css('opacity', '');
            }
        });
    }
}

views.py:

now u can easily access checkboxes by using getlist("checkboxes")