Jquery添加和删除文本框(input)代码

来源:互联网 时间:1970-01-01

本文章给各位总结了两个利用Jquery添加和删除文本框(input)实例,有需要了解的朋友可进入参考。

例1

 代码如下 复制代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Jquery 手动添加和删除文本框</title>

</head>

<script src="jquery-1.6.4.js"></script>

<style>

* { font-family:Arial; }

h2 { padding:0 0 5px 5px; }

h2 a { color: #224f99; }

a { color:#999; text-decoration: none; }a:hover { color:#802727; }
p { padding:0 0 5px 0; }

input { padding:5px; border:1px solid #999; border-radius:4px; -moz-border-radius:4px; -web-kit-border-radius:4px; -khtml-border-radius:4px; }
</style>
<body>
<h2><a href="#" id="addScnt">点击添加</a></h2>

<div id="p_scents">
    <p>
        <label for="p_scnts"><input type="text" id="p_scnt" size="20" name="p_scnt" value="" /></label>
    </p>
</div>
<script>
$(function() {
        var scntDiv = $('#p_scents');
        var i = $('#p_scents p').size() + 1;

        $('#addScnt').live('click', function() {
                $('<p><label for="p_scnts"><input type="text" id="p_scnt" size="20" name="p_scnt_' + i +'" value="" /></label> <a href="#" id="remScnt">删除</a></p>').appendTo(scntDiv);
                i++;
                return false;
        });

        $('#remScnt').live('click', function() {
                if( i > 2 ) {
                        $(this).parents('p').remove();
                        i--;
                }
                return false;
        });
});

</script>
</body>
</html>

例2

 代码如下 复制代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
 $(document).ready(
  function(){  
$("#theButton").click(
function (){
   //创建三个元素
           var $txt = $("<input type='text' value='' size='20' name='theText' />");
                var $btn = $("<input type='button' value='del'/>"); 
                var $br = $("<br/>"); 
$btn.click( //设置删除按钮的onclick事件
function (){
  $txt.remove();
  $btn.remove();
  $br.remove();
  }
)
   $("#div1").append($txt).append($btn).append($br);
}
)
 }
 )
</script>
<title>无标题文档</title>
</head>
<body>
 <input type="button" value="add" id="theButton" />
 <div id="div1"></div>
 
</body>
</html>




相关阅读:
Top