简体   繁体   English

动态文本字段不能取值jquery php

[英]Dynamic text field can't take the value jquery php

I'm trying to add/remove items dynamically but I can't take the values of all the elements of the array. 我正在尝试动态添加/删除项目,但无法获取数组所有元素的值。

Basically I have this form 基本上我有这个表格

<form action="" method="post">
    <div class="input_fields_wrap">
        <div>
            <label> <span>Team name </span> 
            <input type="text" class="input-field" name="teams[]"> </label>
        </div>
    </div>
    <span id="num_teams"></span> <label><span>&nbsp;</span>
    <input type="submit" value="Add Team" class="add_field_button" name="add_field_button">
    <input type="submit" name="next" value="Next" />
    </label> 
</form>

It just shows an input box where I'd have to insert the team name and two buttons ; 它只是显示一个输入框,我必须在其中输入团队名称和两个按钮; one to go to the next page, and the other one to add a new text field using jquery. 一个可以转到下一页,另一个可以使用jquery添加新的文本字段。 Here it is the jquery script 这是jQuery脚本

<script type="text/javascript">
$(document).ready(function() {



    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_field_button"); //Add button ID

    var x = 1; //initlal text box count
    $(add_button).click(function(e){ //on add input button click
    e.preventDefault();
var max_fields = $('#n_teams').val(); //maximum input boxes allowed
    if(x < max_fields){ //max input box allowed
        x++; //text box increment

        $(wrapper).append('<div><label><span>Team Name </span><input type="text" class="input-field" name="teams[]">  <a href="#" class="remove_field">Delete</a></label></div>'); // add input box
        $("#num_teams").html('Number of Teams: '+x);

    }
});

$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
    e.preventDefault(); $(this).parent('label').remove(); x--;
    $("#num_teams").html('Number of Teams: '+x);
})
});
</script>

The script above works perfectly: it adds and removes textfields. 上面的脚本运行完美:它添加和删除了文本字段。

The problem that I have now is that I can't take the values of the array 'teams[]' . 我现在遇到的问题是我不能接受数组'teams []'的值。 in a 在一个

if(isset($_POST['next'])) 

even if I try to take the values manually like 即使我尝试像这样手动取值

echo $_POST["teams"][0]; and 
echo $_POST["teams"][1]; ect...

It just takes the first value (ie the one I don't add using jquery). 它只是采用第一个值(即我没有使用jquery添加的值)。 It doesn't 'see' the jquery text fields added. 它不会“看到”添加的jquery文本字段。

Of course my final aim is to insert 'teams[]' in a mysql table, but for now I noticed that I can't either take the values. 当然,我的最终目标是在mysql表中插入“ teams []”,但是现在我注意到我不能选择任何值。

Where am I wrong ? 我哪里错了?

EDIT - SOLVED It was a very stupid error I made in the html code. 编辑-解决这是我在html代码中犯的一个非常愚蠢的错误。 Actually there was a <div> before of the <form> that caused all the troubles. 实际上,在<form>之前有一个<div>引起了所有麻烦。 After a very accurate analysis , trying every single piece of code alone, I finally got that I just had to move the <form> above two <div> to make the code work. 经过非常精确的分析,仅尝试每一个代码段,我最终发现我只需要将<form>移到两个<div>上方即可使代码正常工作。 I do apologize to everyone, silly me! 我向所有人道歉,愚蠢的我!

Instead of using name="teams[]" use name="teams" and on server side use: 除了使用name="teams[]"使用name="teams"并在服务器端使用:

$_POST['teams']

which should give you the comma separated list. 这应该给您逗号分隔的列表。

var wrapper = $(“。input_fields_wrap”)。first();

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

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