dou12754 2013-01-14 07:35
浏览 49

Javascript,克隆,功能和更改ID和名称,任何想法?

Here is my Javascript:

<script>

        function disable()
        {
            document.getElementById("habits").disabled=true;
            document.getElementById("habits2").disabled=true;

            document.getElementById("exact").disabled=false;
        }
        function enable()
        {
            document.getElementById("habits").disabled=false;
            document.getElementById("habits2").disabled=false;

            document.getElementById("exact").disabled=true;
        }
        var counter =0;
        var i = 0;
        function duplicate() { 
            var original = document.getElementById('div');
            var clone = original.cloneNode(true); // "deep" clone

            i+=1;
            clone.id = "div" + i; // there can only be one element with an ID
            original.parentNode.appendChild(clone);

            document.getElementById('div' + i).getElementsByTagName('select').name += '_clone' + i;
            counter+=1;
        }

    </script>

and this is my HTML code:

 <button id="button" onclick="duplicate()">Add List</button><br><br>
        <form id ="form" name="search" method="post" action="test.php">
            <div id="div">
                <div id="d">
                    <select name ="select" >
                        ...options...
                    </select>
                </div>

                Value:<input type="text" id ="exact">

                From: <input type="text" id="habits">
                To: <input type="text" id="habits2">

                <br>
                <input type="button" name="answer" value="Range" onclick="enable()"  >
                <input type="button" name="answer" value="Exact" onclick="disable()" >
            </div>
            <br><br>

            <input type="submit" name ="search" value="Submit">
        </form>

My issue here is that, when I clone the div id=div, all the buttons work for the original one, even the cloned buttons. Another thing is that, when I click the submit button to get the options from the drop-down list(s), but after submission, only the last drop-list is counted (cloned), but I want the original only.

Here is my page after clicking submit:

<?php
$item = $_POST["select"];
echo $item;
?>

How can I solve this? That is, changing the id's and names, and functions working with the cloned elements?

  • 写回答

2条回答 默认 最新

  • duanjian7617 2013-01-14 07:54
    关注

    First of all, don't use id's if you want to duplicate things. Use and select by class (I strongly recommend to use jQuery), and use name="some_name[]"

    To solve your problem you can do: onclick="javascript:enable(this)" or, if you decide to use jQuery, use `onclick="javascript:jQuery(this)", and then you search for siblings of the current element (don't know the pure js syntax, just the jquery one, if you need help with jquery let me know).

    Hope this helps, good luck.

    评论

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法