Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/415.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从HTML到Ajax请求中获取选中的复选框?_Javascript_Php_Jquery_Html_Ajax - Fatal编程技术网

Javascript 如何从HTML到Ajax请求中获取选中的复选框?

Javascript 如何从HTML到Ajax请求中获取选中的复选框?,javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,我有一个页面,其中可能的选择表是通过Ajax调用使用Javascript动态生成的。表的每一行都有一个复选框,其中包含一个关联的id。用户可以选择任意多个复选框,然后按下“开始”按钮。当按下按钮时,我需要获取选择并将其作为Ajax调用数据的一部分发送。我陷入困境的地方是获取选择并在Ajax调用中使用它们 index.html: <!DOCTYPE html> <html> <head> <title>My Web Page&

我有一个页面,其中可能的选择表是通过Ajax调用使用Javascript动态生成的。表的每一行都有一个复选框,其中包含一个关联的
id
。用户可以选择任意多个复选框,然后按下“开始”按钮。当按下按钮时,我需要获取选择并将其作为Ajax调用数据的一部分发送。我陷入困境的地方是获取选择并在Ajax调用中使用它们

index.html

<!DOCTYPE html>
<html>
    <head>
        <title>My Web Page</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
        <script src="access_database.js" type="text/javascript"></script>
   </head>
   <body>
        <script>initializeDatabase();</script>
        <h1 align="center">WELCOME TO MY APPLICATION</h1>
        <script>getDecks();</script>
        <p>FRONT</p><input type="radio" name="side_selection" value="FRONT">
        <p>BACK</p><input type="radio" name="side_selection" value="BACK">
        <br></br>
        <form action="study.html">
            <input type="submit" value="START">
        </form>
   </body>
</html>

我希望使用我现在所拥有的来获取选择,然后使用Ajax调用中的数据进行查询。我只是想寻求帮助,以一种可以用于Ajax调用的格式获取选择

我认为您正在寻找类似的内容(免责声明:未运行/语法检查)

$('input[type=“submit”])。单击(函数(){
变量选择=[]
for(var i=0;i
我知道你在寻求具体的帮助,但我觉得在道义上有义务告诉你,使用javascript是非常不正确的。具体地说,我是说

    <script>initializeDatabase();</script>
    <h1 align="center">WELCOME TO MY APPLICATION</h1>
    <script>getDecks();</script>
初始化为tabase();
欢迎参加我的申请
getDecks();

但是您创建表的方法也非常长/乏味。

您可以这样做

function getChecked() {
    return $('table input[type="checkbox"]').filter(':checked').map(function(index, item) {
        return $(item).attr('id');
    }).toArray();
}

一点提示:如果您使用jquery,那么创建表就容易多了:
$('foo>bar')。appendTo(…)
您能简要解释一下javascript不正确的原因吗?好的。简单的规则是没有内联javascript。如果需要运行javascript,请在页面加载结束时将其包含在内,所有内容都打包在一个
    <script>initializeDatabase();</script>
    <h1 align="center">WELCOME TO MY APPLICATION</h1>
    <script>getDecks();</script>
function getChecked() {
    return $('table input[type="checkbox"]').filter(':checked').map(function(index, item) {
        return $(item).attr('id');
    }).toArray();
}