[Python] Having trouble with ajax and returning checkbox state(s) and textbox contents to...

Discussão em 'Python' iniciado por Stack, Abril 14, 2025.

  1. Stack

    Stack Membro Participativo

    Wondered if anyone could help as I'm having a bit of trouble. I can successfully return the state of an array of checkboxes to Python/Flask. But, I cannot for the life of me return the textbox contents at the same time. What does work:

    HTML

    <label class="checkboxes">
    <input type="checkbox" name="headers" id="test1" class="styled-checkbox">Ch-Test5
    <input type="checkbox" name="headers" id="test2" class="styled-checkbox">Ch-Test5
    <input type="checkbox" name="headers" id="test3" class="styled-checkbox">Ch-Test5
    <input type="checkbox" name="headers" id="test4" class="styled-checkbox">Ch-Test5
    <input type="checkbox" name="headers" id="test5" class="styled-checkbox">Ch-Test5
    </label>
    <input type="text" id="lookup" name="fname" style="margin-left: 30px">


    ajax

    script type="text/javascript">
    $(function() {
    $('#submit_button').click(function(e) {
    e.preventDefault();
    $.ajax({
    async: false,
    data: $('[name="headers"]').serialize(),
    type: "POST",
    url : '/search_submit',
    })
    });
    });


    PYTHON

    Use the getlist to retreive the selected checkboxes as a list

    if request.method == "POST":
    headers = request.form.getlist("headers")
    print("Selected checkboxes ",headers)


    I cannot however get/POST the textbox contents using ajax at the same time. I'm not familiar with java/ajax at all so struggling to find out what ajax query to use to collect the textbox at the same time as the checkboxes (in the same query, when clicking the Submit button).

    Any help appreciated Many thanks

    Continue reading...

Compartilhe esta Página