Javascript按递增顺序添加新输入字段

时间:2014-03-17 18:41:58

标签: javascript php jquery html

我无法让我的javascript向getElementById添加变量。我正在做的是允许用户每个帖子有10位作者。该脚本显示作者已经进入系统,现在我试图允许他们仅在10个字段显示的位置添加更多输入字段。这是我到目前为止的代码:

$y=1;
//GET EXISTING AUTHORS FOR POST
while ($getauthorsrow=$getauthorssql->fetch()) {
    $showauthor = $getauthorsrow['author'];
    $authorid = $getauthorsrow['ID'];
    echo "<input type='text' value='$showauthor' name='authorname[]'>
    <input type='hidden' value='$authorid' name='authorid[]'><br><br>";
    $y++;
}
$newy=$y-1;
?>

//SCRIPT TO ADD NEW FIELD ONLY TO THE POINT OF 10
<script language="javascript">
fields = <?php echo $newy; ?>;
    function addInput<?php echo $i; ?>() {
    if (fields != 10) {

        //HERE I'M TELLING IT TO GET text + $i (which is post number) + fields variable
        //So if it's the first post and the new field divs start a 5
        //Then it should getElementByid('text05') and continue on.

        document.getElementById('text<?php echo $i; ?>' + fields).innerHTML += "<center><input type='text' name='newauthorname[]'></center><br /><br />";
        fields += 1;
    } else {
        document.getElementById('text<?php echo $i; ?>' + fields).innerHTML += "<br />Only 10 authors allowed.";
        document.ajaxform<?php echo $i; ?>.add<?php echo $i; ?>.disabled=true;
    }
}
</script>

//HERE I'M ADDING THE NEW DIVS FOR TEXT FIELDS AND STARTING
//THEM AT WHATEVER NUMBER POST $i IT IS AND WHATEVER THE $w is.
//SO IN THE EXAMPLE IN THE JAVASCRIPT IT'D SAY id='text05'
//THIS WORKS JUST FINE
<?php
$w=$newy;
while ($w<=10) {
    echo "<div id='text".$i.$w."'></div>";
    $w++;;
}

//ECHO THE ADD AUTHOR BUTTON
echo "
<input type='button' onclick='addInput$i()' name='add$i' value='Add Author'><br>
<input type='hidden' name='count' value='$newy'>
<input type='hidden' name='id' value='$fileid'>
<input type='submit'>
</form>
";
?>

$ i是从0开始的帖子编号。使得div工作正常的PHP。我只是在javascript中为getElementById获取null。我在这里弄错了什么?

HTML输出示例:

<form id="ajaxform0">
    <input type="text" value="Logan" name="authorname[]">
        <input type="hidden" value="121" name="authorid[]"><br><br><input type="text" value="Matt" name="authorname[]">
        <input type="hidden" value="122" name="authorid[]"><br><br><input type="text" value="Chad" name="authorname[]">
        <input type="hidden" value="123" name="authorid[]"><br><br><input type="text" value="hey" name="authorname[]">
        <input type="hidden" value="128" name="authorid[]"><br><br><input type="text" value="jordan" name="authorname[]">
        <input type="hidden" value="129" name="authorid[]"><br><br>    
    <script language="javascript">
    fields = 5;
        function addInput0() {
        if (fields != 10) {
            var currentText = 'text0' + fields;
            document.getElementById(currentText).innerHTML += "<center><input type='text' name='newauthorname[]'></center><br /><br />";
            fields += 1;
        } else {
            document.getElementById(currentText).innerHTML += "<br />Only 10 authors allowed.";
            document.ajaxform0.add0.disabled=true;
        }
    }
    </script>

    <div id="text05"></div><div id="text06"></div><div id="text07"></div><div id="text08"></div><div id="text09"></div><div id="text010"></div>
    <input type="button" onclick="addInput0()" name="add0" value="Add Author"><br>
    <input type="hidden" name="count" value="5">
    <input type="hidden" name="id" value="45">
    <input type="submit">
    </form>

3 个答案:

答案 0 :(得分:0)

首先尝试将该连接字符串分配给JS变量,然后调用函数:

if (fields != 10) {

    var currentText = 'text<?php echo $i; ?>' + fields;
    document.getElementById(currentText).innerHTML += "<center><input type='text' name='newauthorname[]'></center><br /><br />";
    fields += 1;
} else {
    document.getElementById(currentText).innerHTML += "<br />Only 10 authors allowed.";
    document.ajaxform<?php echo $i; ?>.add<?php echo $i; ?>.disabled=true;
}

答案 1 :(得分:0)

我需要将$ i添加到javascript中的fields变量中。有多个文件,它正在做同一个变量的倍数...我工作。

答案 2 :(得分:0)

您好我修改了您的html和JavaScript代码。

<form id="ajaxform0">
<input type="text" value="Logan" name="authorname[]">
    <input type="hidden" value="121" name="authorid[]"><br><br><input type="text" value="Matt" name="authorname[]">
    <input type="hidden" value="122" name="authorid[]"><br><br><input type="text" value="Chad" name="authorname[]">
    <input type="hidden" value="123" name="authorid[]"><br><br><input type="text" value="hey" name="authorname[]">
    <input type="hidden" value="128" name="authorid[]"><br><br><input type="text" value="jordan" name="authorname[]">
    <input type="hidden" value="129" name="authorid[]"><br><br>
<script language="javascript">
fields = 5;
    function addInput0() {
    if (fields != 10) {
        var currentText = 'text0' + fields;
        document.getElementById(currentText).innerHTML += "<center><input type='text' name='newauthorname[]'></center><br /><br />";
        fields += 1;
    } else {
        var myout = "Only 10 authors allowed.";
        document.getElementById("stat").innerHTML = myout;
    }
}
</script>

<div id="text05"></div><div id="text06"></div><div id="text07"></div><div id="text08"></div><div id="text09"></div><div id="text010"></div><div id="stat"></div>
<input type="button" onclick="addInput0()" name="add0" value="Add Author"><br>
<input type="hidden" name="count" value="5">
<input type="hidden" name="id" value="45">
<input type="submit">

检查一下,如果已经解决,请告诉我。