-3

一定是犯了一个小错误。我研究过一个 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>

查询:

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

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

1 回答 1

0

there were so many mistakes... first you should learn how to debug, install firebug and use console.log(), to check if you are accessing the the click's to see, if you are getting the selcted item...

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

much worse:

$("#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...
});

here is the fiddle study it..: And, script tag inside a select? no need for that:...

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

于 2013-09-02T11:16:49.340 回答