點擊追加以及刪除

<form method="post" action="obj.php">
    <input type="hidden" name="habays" id="habays" value=""  />
    <div id="objDiv" style="float: left;"></div>
    <input type="text" style="width:200px;" name="objIn" id="objIn" value=""  />
    <input type="button" id="copy" value="添加" />
    <input type="submit" value="提交">
</form>
<script type="text/javascript">
    var objIn=document.getElementById("objIn");
    var objDiv=document.getElementById("objDiv");
    var objSave = document.getElementById("habays");
    $("#copy").on("click",function(){    
        objDiv.innerHTML+="<input type='hidden' name='habay[]' value='"+(objIn.value)+"'/><span onclick='javascript:removeUrl(this)' style='margin-right:10px;'>"+(objIn.value)+"</span>";
        objSave.value += objIn.value+',';
        $('#objIn').val('');
    });
    function removeUrl(Obj){
        var pUrl = jQuery.trim(jQuery(Obj).text());
        jQuery("input[name='habay[]']").each(function(){
            if(jQuery(this).val()==pUrl){
                jQuery(this).remove();
            }
        });
        Obj.remove();
        Array.prototype.remove=function(dx){
            if(isNaN(dx)||dx>this.length){return false;}
            for(var i=0,n=0;i<this.length;i++){
                if(this[i]!=this[dx]){
                    this[n++]=this[i]
                }
            }
            this.length-=1
        }
        var saveObj = objSave.value;
        var saveObjarr = saveObj.split(",");
        var i = 0;
        for (var i; i < saveObjarr.length; i++) {
            if(saveObjarr[i] == pUrl) {
                saveObjarr.remove(i);
            }
        }
        objSave.value = saveObjarr;
    }
</script>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章