Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在jquery和php中添加或删除输入字段_Javascript_Php_Jquery_Html - Fatal编程技术网

Javascript 在jquery和php中添加或删除输入字段

Javascript 在jquery和php中添加或删除输入字段,javascript,php,jquery,html,Javascript,Php,Jquery,Html,我一直在尝试使用JQuery函数来添加或删除表单输入文本字段。我一直在使用一个我发现的例子作为起点 这是我到目前为止的资料 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml`enter code here`1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xht

我一直在尝试使用JQuery函数来添加或删除表单输入文本字段。我一直在使用一个我发现的例子作为起点

这是我到目前为止的资料

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml`enter code here`1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>

    <script type="text/javascript" src="jquery-1.3.2.min.js"></script>
    <script type="text/javascript">

    function valid_to(txt1id) {

        var toval = document.getElementById(txt1id).value;

        if(toval == "") {
            alert("Text Field1 is Empty");
            document.getElementById(txt1id).focus();
            return false;
        } 
    }

    function calc_from(txt2id) {
        if(document.getElementById(txt2id).value == "") {
            alert("Text Field2 is Empty");
            document.getElementById(txt2id).focus();
            return false;
        }
    }

    </script>

    <script type="text/javascript">
    $(document).ready(function(){

        var max = 10, append_data, tabindex = 23;

        /*If the add icon was clicked*/
        $(".add").live('click',function(){
            var divid = parseInt(document.getElementById("txt_rowcnt").value)+parseInt(1);
            var id = parseInt(document.getElementById("txt_rowcnt").value)+parseInt(1);
            if($("div[id^='txt_']").length <10){ //Don't add new textbox if max limit exceed
                $(this).remove(); //remove the add icon from current text box
                var tabindex2 = parseInt(tabindex)+1;

                var append_data = '<div id="txt_'+divid+'" class="txt_div" style="display:none;"><div class="left"><input type="text" id="txt1_'+id+'" name="txt1_'+id+'" class="medium_input" dir="rtl" style="width:94px;" readonly="true" tabindex="'+tabindex+'" onblur="valid_to(this.id);"/><input type="text" id="txt2_'+id+'" name="txt2'+id+'" class="medium_input" dir="rtl" style="width:94px;" maxlength="5" tabindex="'+tabindex2+'" onblur="calc_from(this.id);"/></div><span class="right" style="float: left; margin-left: 7px; margin-top: 7px;"><img src="add.png" class="add"/> <img src="remove.png" class="remove"/></span></div>';
                $("#text_boxes").append(append_data); //append new text box in main div
                $("#txt_"+divid).effect("bounce", { times:3 }, 300); //display block appended text box with silde down
                divid++;
                id = parseInt(id)+1;
                tabindex = parseInt(tabindex)+2;
            } else {
                alert("Maximum 100 textboxes are allowed");
            }
            document.getElementById("txt_rowcnt").value=parseInt(divid)-parseInt(1);
        })

        /*If remove icon was clicked*/
        $(".remove").live('click',function(){
            var prev_obj = $(this).parents().eq(1).prev().attr('id');  //previous div id of this text box
            $(this).parents().eq(1).slideUp('medium',function() { $(this).remove(); //remove this text box with slide up
                if($("div[id^='txt_']").length > 1){
                    append_data = '<img src="remove.png" class="remove"/>'; //Add remove icon if number of text boxes are greater than 1
                }else{
                append_data = '';
                }
                if($(".add").length < 1){
                    $("#"+prev_obj+" .right").html('<img src="add.png" class="add"/> '+append_data);
                    document.getElementById("txt_rowcnt").value=parseInt(document.getElementById("txt_rowcnt").value)-parseInt(1);
                }
            });
        })
    });
    </script>
</head>
<body>
    <form method="post" action="" enctype="multipart/form-data" >
        <table>
            <tr>
                <td> Textboxes </td>
            </tr>
            <tr>
                <td>
                    <div id="text_boxes"> 
                        <div id="txt_1" class="txt_div">
                            <div class="left"> 
                                <input type="text" name="txt1_1" id="txt1_1" tabindex="21" onblur="valid_to(this.id);" />
                                <input type="text" name="txt2_1" id="txt2_1" tabindex="22" onblur="calc_from(this.id);"/> 
                                &nbsp;&nbsp;<span class="right" style="margin-top:7px;"> <img src="add.png" class="add"/> </span> 
                            </div>
                        </div>
                    </div>
                </td>
            </tr> 
            <input type="hidden" name="txt_rowcnt" id="txt_rowcnt" value="1" />
        </table> 
    </form>
</body>
</html>

无标题文件
函数有效到(txt1id){
var toval=document.getElementById(txt1id).value;
如果(toval==“”){
警报(“文本字段1为空”);
document.getElementById(txt1id.focus();
返回false;
} 
}
函数计算从(txt2id){
if(document.getElementById(txt2id).value==“”){
警报(“文本字段2为空”);
getElementById(txt2id).focus();
返回false;
}
}
$(文档).ready(函数(){
var max=10,追加_数据,tabindex=23;
/*如果单击了添加图标*/
$(“.add”).live('click',function(){
var divid=parseInt(document.getElementById(“txt_rowcnt”).value)+parseInt(1);
var id=parseInt(document.getElementById(“txt_rowcnt”).value)+parseInt(1);
如果($(“div[id^='txt_'])。长度请尝试以下操作:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml`enter code here`1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>

<script type="text/javascript" src="jquery1.9.1.js"></script>
</head>
<script type="text/javascript">

function valid_to(txt1id) {

    var toval = document.getElementById(txt1id).value;

    if(toval == "") {
        alert("Text Field1 is Empty");
        document.getElementById(txt1id).focus();
        return false;
    } 
}

function calc_from(txt2id) {
    if(document.getElementById(txt2id).value == "") {
        alert("Text Field2 is Empty");
        document.getElementById(txt2id).focus();
        return false;
    }
}

</script>

<script type="text/javascript">
$(document).ready(function(){

    var max = 10, append_data, tabindex = 23;

    /*If the add icon was clicked*/
    $(".add").click(function(){
        alert('test');
        var divid = parseInt(document.getElementById("txt_rowcnt").value)+parseInt(1);
        var id = parseInt(document.getElementById("txt_rowcnt").value)+parseInt(1);
        if($("div[id^='txt_']").length <10){ //Don't add new textbox if max limit exceed
        $(this).remove(); //remove the add icon from current text box
        var tabindex2 = parseInt(tabindex)+1;

        var append_data = '<div id="txt_'+divid+'" class="txt_div" style="display:block;"><div class="left"><input type="text" id="txt1_'+id+'" name="txt1_'+id+'" class="medium_input" dir="rtl" style="width:94px;" readonly="true" tabindex="'+tabindex+'" onblur="valid_to(this.id);"/><input type="text" id="txt2_'+id+'" name="txt2'+id+'" class="medium_input" dir="rtl" style="width:94px;" maxlength="5" tabindex="'+tabindex2+'" onblur="calc_from(this.id);"/></div><span class="right" style="float: left; margin-left: 7px; margin-top: 7px;"><img src="add.png" class="add"/> <img src="remove.png" class="remove"/></span></div>';
        $("#text_boxes").append(append_data); //append new text box in main div
        //$("#txt_"+divid).effect("bounce", { times:3 }, 300); //display block appended text box with silde down
        divid++;
        id = parseInt(id)+1;
        tabindex = parseInt(tabindex)+2;
        } else {
            alert("Maximum 100 textboxes are allowed");
        }
        document.getElementById("txt_rowcnt").value=parseInt(divid)-parseInt(1);
    })

/*If remove icon was clicked*/
    $(".remove").click(function(){
        var prev_obj = $(this).parents().eq(1).prev().attr('id');  //previous div id of this text box
        $(this).parents().eq(1).slideUp('medium',function() { $(this).remove(); //remove this text box with slide up
        if($("div[id^='txt_']").length > 1){
            append_data = '<img src="remove.png" class="remove"/>'; //Add remove icon if number of text boxes are greater than 1
        }else{
            append_data = '';
        }
        if($(".add").length < 1){
            $("#"+prev_obj+" .right").html('<img src="add.png" class="add"/> '+append_data);
            document.getElementById("txt_rowcnt").value=parseInt(document.getElementById("txt_rowcnt").value)-parseInt(1);
        }
        });
    })

});
</script>
<body>
<form method="post" action="" enctype="multipart/form-data" >
<table> <tr>
<td> Textboxes </td> </tr>
<tr> <td> <div id="text_boxes"> 
<div id="txt_1" class="txt_div">
<div class="left"> 
<input type="text" name="txt1_1" id="txt1_1" tabindex="21" onblur="valid_to(this.id);" />
<input type="text" name="txt2_1" id="txt2_1" tabindex="22" onblur="calc_from(this.id);"/> 
&nbsp;&nbsp;<span class="right" style="margin-top:7px;"> <img src="add.png" alt="add" class="add"/> </span> 
</div> </div> </div> </td>
 </tr> 
               <input type="hidden" name="txt_rowcnt" id="txt_rowcnt" value="1" />
</table> 
</form>
</body>
</html>

无标题文件
函数有效到(txt1id){
var toval=document.getElementById(txt1id).value;
如果(toval==“”){
警报(“文本字段1为空”);
document.getElementById(txt1id.focus();
返回false;
} 
}
函数计算从(txt2id){
if(document.getElementById(txt2id).value==“”){
警报(“文本字段2为空”);
getElementById(txt2id).focus();
返回false;
}
}
$(文档).ready(函数(){
var max=10,追加_数据,tabindex=23;
/*如果单击了添加图标*/
$(“.add”)。单击(函数(){
警报(“测试”);
var divid=parseInt(document.getElementById(“txt_rowcnt”).value)+parseInt(1);
var id=parseInt(document.getElementById(“txt_rowcnt”).value)+parseInt(1);

如果($(“div[id^='txt\'])).length sry我是PHP新手..这是一个js文件..为什么不需要tat?这是一个非常旧的版本…如果我拿出jquery-1.3.2,新的是1.10.2k,我可以得到输出吗?我觉得很好,我需要做int-tat链接吗?如果这个问题不太明确,我会提前sry的。我可以找到jquery1.9.1.js吗?在这里你可以找到最新的版本。创建一个名为jq的文件uery-1.10.2.js,复制并粘贴jquery-1.10.2.js中的代码