添加和删​​除克隆?

时间:2013-09-02 10:56:42

标签: jquery

一定是犯了一个小错误。我有一个jsfiddle工作。我试图让添加和删除按钮工作。

他们还有一个我没有包含的隐藏和显示元素,但它应该没关系?

http://jsfiddle.net/27N7Y/

HTML:

<div class="question">
<div class="questiontext">
    23. Have you or anyone included in this application ever previously applied for a visa or travelled to Australia?
</div>
<div class="questionradioanswer">
    No <input type="radio" id="notravel" name="previousvisa" value="No" class="radiobutton">
    Yes <input type="radio" id="yestravel" name="previousvisa" value="Yes" class="radiobutton">
    </div>
    <div id="question23hide">
        Provide dates of previous visits to Australia<br>
        <select id="selectday6" class="daydate">
            <option>Day</option>
            <script>
                var select = document.getElementById("selectday6");
                var options = new Array();
                var temp = 1;
                for (var i = 0; i < 31; i++) {
                options.push(temp);
                temp++;
                }
                for (var i = 0; i < options.length; i++) {
                var opt = options[i];
                var el = document.createElement("option");
                el.textContent = opt;
                el.value = opt;
                select.appendChild(el);
                }
            </script>
        </select>
        <select id="selectmonth6" class="monthdate">
            <option>Month</option>
            <script>
                var select = document.getElementById("selectmonth6");
                var options = ["January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
                for (var i = 0; i < options.length; i++) {
                var opt = options[i];
                var el = document.createElement("option");
                el.textContent = opt;
                el.value = opt;
                select.appendChild(el);
                }
            </script>
        </select>
        <select id="selectyear6" class="yeardate">
            <option>Year</option>
            <script>
                var select = document.getElementById("selectyear6");
                var options = new Array();
                var firstyear = (new Date().getFullYear()) - 18;
                var temp = firstyear;
                for (var i = 0; i < 83; i++) {
                options.push(temp);
                temp--;
                }
                for (var i = 0; i < options.length; i++) {
                var opt = options[i];
                var el = document.createElement("option");
                el.textContent = opt;
                el.value = opt;
                select.appendChild(el);
                }
            </script>
        </select>
        </div>
        <div id="adddeleteprevious">
            <div id="addprevious">
                <input type="button" class="button" value="Add Previous Visits">
                </div>
                <div id="deleteprevious">
                    <input type="button" class="button" value="Delete Previous Visits">
                    </div>
                </div>
            </div>

JQUERY:

$("#addprevious").click(function(){
    $(".question23hide:last").after($(".question23hide:first").clone(true));
});

$("#deleteprevious").click(function() {
    if($(".question23hide").length!=1)
        $(".question23hide:last").remove();
});

1 个答案:

答案 0 :(得分:0)

错误太多了...... 首先你应该学习如何调试,安装firebug并使用console.log()来检查你是否正在访问点击,看看,如果你得到了所选项目......

console.log($(".question23hide:last")) 
//Result no Object matched, oh you try to access id not class..
更糟糕的是:

$("#addprevious").click(function(){
      console.log("am i clicked"); 
      //no! you try to click on a div, but you want to click on the button inside...
});

这里是小提琴研究它..: 而且,选择内的脚本标签?没有必要:......

http://jsfiddle.net/V7yp6/1/