简体   繁体   English

数据未传递给Ajax函数

[英]Data not getting passed to the ajax function

The form below is present in every row of an html table.However alert("dfd") pops up only for the first row of the table and not for the forms in the other rows(these forms appear on the webpage).Can anyone tell why is it happening? html表格的每一行中都存在下面的表格。但是,警报(“ dfd”)仅针对表格的第一行弹出,而其他行中的表格则不弹出(这些表格显示在网页上)。告诉为什么会这样?

HTML: HTML:

 <form class='form form-horizontal' method='post' role='form' id='formsel' name='formsel'>
        <fieldset  >   
        <div>    
            <span class='control-group' >
            <span  class='controls'>
                <select id='formcont' name='formcont' class='form-control' style='cursor:pointer;'>
                    <option selected='selected' style='display:none;'>Select</option>
                    <option  value='1'>Option1</option>
                    <option  value='2'>Option2</option>
                    <option  value='3'>Option3</option>

            </select> <button id='selbtn' name='selbtn' type='submit'>Sub,it</button>

            </span>
            </span></div>

        </fieldset>

    </form>

JS: JS:

$("#formsel").submit(function () {
    alert("dfd");
    $.ajax({
        url:"add.php",
        data:$("#formcont").val().serialize(),
        type:"POST",
        success:function(data){
            console.log(data);
            if(data=="done"){
                alert("success");
            } 
        },
        error:function(data){
            alert("Network ERROR");
        }
    })
    return false;
});


$("#selbtn").click(function () {
  ("#formsel").submit();
});

PHP: PHP:

 <?php
    include ('db.php');
    session_start();

    echo "done";
    ?>

Yes, because you're not sending any data: 是的,因为您没有发送任何数据:

$("#formcont").val().serialize()

Should be: 应该:

$(this).serialize()

Since the button you're clicking to trigger the submit is a submit button, you really do not need the following code: 既然你点击触发按钮submit是一个提交按钮,你真的不需要下面的代码:

$("#selbtn").click(function () {
  ("#formsel").submit();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM