dongsheng9203 2014-07-08 10:57
浏览 20

如何添加链接以使用ajax消除一组框

I am having trouble with this code. I am trying to add an eliminate link at the end but I cant seem to eliminate all of the boxes within the set of 5 that appear when I press the add link. I want to be able to delete all the boxes that are within the set of five and nothing else. What I have tried to do is put this line of code at the end of the var box_html5 but it does not work. This is the function that deletes a box I want it to delete the boxes.

<a href="#" class="remove-box">Remove</a>

I have tried to make a loop within the statement but it is not coming out right.

I have also thought about making an if statement where the above piece of code would be inside it and state that if that link is pressed then remove box_html1, box_html2, box_html3... etc. For the set of 5 chosen. I have also tried to echo the code like this:

echo ('<a href="#" class="remove-box">Remove</a>');

and it still does not seem to work.

<?php
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript" src="//code.jquery.com/jquery-latest.js"></script>
<style type="text/css">
<!--
#main {
    max-width: 800px;
    margin: 0 auto;
}
-->
</style>
</head>
<body>
<div id="main">
    <h1>Movie Night</h1>
    <div class="my-form">
        <form role="form" method="post">
            <p class="text-box">
                <label for="box1">Genre<span class="box-number">1</span></label>
                <input type="text" name="boxes[]" value="" id="box1" />
                <a class="add-box" href="#">Add More</a>
            </p>
            <p><input type="submit" value="Submit" /></p>
        </form>
    </div>
</div>
<script type="text/javascript">
jQuery(document).ready(function($){
    $('.my-form .add-box').click(function(){
        var n = $('.text-box').length + 1;
        if( 15 < n ) {
            alert('Too many. Not enought Time to watch');
            return false;
        }
        var box_html1 = $('<p class="text-box">Movie<input name="mov1" type="text"></p>');
        box_html1.hide();
        $('.my-form p.text-box:last').after(box_html1);
        box_html1.fadeIn('slow');

        var box_html2 = $('<p class="text-box">Movie<input name="mov2" type="text"></p>');
        box_html2.hide();
        $('.my-form p.text-box:last').after(box_html2);
        box_html2.fadeIn('slow');

        var box_html3 = $('<p class="text-box">Movie<input name="mov3" type="text"></p>');
        box_html3.hide();
        $('.my-form p.text-box:last').after(box_html3);
        box_html3.fadeIn('slow');

        var box_html4 = $('<p class="text-box">Movie<input name="mov4" type="text"><a href="#" class="remove-box">Remove</a></p>');
        box_html4.hide();
        $('.my-form p.text-box:last').after(box_html4);
        box_html4.fadeIn('slow');

        var box_html5 = $('<p class="text-box">Movie<input name="mov5" type="text"></p>');
        box_html5.hide();
        $('.my-form p.text-box:last').after(box_html5);
        box_html5.fadeIn('slow');

        <!--<a href="#" class="remove-box">Remove</a>-->
        return false;
    });
    $('.my-form').on('click', '.remove-box', function(){
        $(this).parent().css( 'background-color', '#FF6C6C' );
        $(this).parent().fadeOut("slow", function() {
            $(this).remove();
            $('.box-number').each(function(index){
                $(this).text( index + 1 );
            });
        });
        return false;
    });
});
</script>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • doucheng1891 2014-07-08 11:42
    关注

    You need to explore index and slice jquery functions.

    Remove action handler in this case:

    $('.my-form').on('click', '.remove-box', function(event)
    {
        var index = $(this).parent().index();
        $('.text-box').slice( index - 3, index + 2 ).remove();
        event.preventDefault();
    });
    

    FIDDLE: http://jsfiddle.net/ejcXd/4/

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题