单击“提交”后将复选框值传递给js

时间:2018-06-29 00:41:37

标签: javascript html

我有一堆通过id ='checkboxes'的js函数从数据库查询的复选框。我正在尝试将检查的值传递给js函数,但控制台返回了未定义的终结点(即.... /未定义的HTTP / 1.1 200-)

HTML:

 <div class="quote"><h1>WOW YOUR AUDIENCE<br></h1><hr style="margin: 0 20%;">

Javascript:

<form action="javascript:getQuestionData(this.value)" method="post">
        <div id="checkboxes">
        </div>
        <input type='submit' value="Submit">
        </input>
  </form>

};

为什么未将检查值传递给getQuestionData函数?

1 个答案:

答案 0 :(得分:0)

在“提交”按钮中添加以下代码:

onclick='return getQuestionData();

function getQuestionData() {
    

    var question = document.getElementsByName("question")[0].checked;
    alert(question);
    

    var endPointQuestionData = '/api/v1/questions/' + question
    Plotly.d3.json(endPointQuestionData, function(error, response) {

        if (error) return console.warn(error);

        appendInnerHTML(response)
    });
};
<form method="post">
            <input type="checkbox" name="question" value = "Question"> Question<br>
            <input type="checkbox" name="question" value = "Solve"> Solve<br>
            <input type='submit' value="Submit" onclick = "getQuestionData()">

      </form>