簡體   English   中英

Javascript以升序添加新的輸入字段

[英]Javascript Add New Input Field in Increasing Order

我在讓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>

嘗試首先將連接的字符串分配給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;
}

我需要將$ i添加到javascript中的fields變量中。 對於多個文件,它正在做同一個變量的倍數。

嗨,我已經為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">

檢查它,如果解決了,請通知我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM