Home > Web Front-end > JS Tutorial > body text

jQuery dynamically adds

高洛峰
Release: 2017-02-11 17:03:46
Original
1429 people have browsed it

Sometimes it is necessary to allow users to upload multiple files on the page. The number is determined by the user. If the number is too large, it can be deleted. This function can be easily achieved using jQuery.

Let me show you the renderings first:

jQuery动态添加<input type=" src="https://img.php.cn/upload/article/000/000/013/f187c49b58a89f7220e072b8c98dfa73-0.jpg" style="max-width:90%" style="max-width:90%" title="jQuery动态添加">

<!DOCTYPE html>
<html>
<head>
<title>test.html</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="jquery-1.6.4.min.js"></script>
<script type="text/javascript">
//添加一行<tr>
function add() {
var content = "<tr><td>";
content += "<input type=&#39;file&#39; name=&#39;file&#39;><input type=&#39;button&#39; value=&#39;Remove&#39; onclick=&#39;remove(this)&#39;>";
content +="</td></tr>"
$("#fileTable").append(content);
}
//删除当前行<tr>
function remove(obj) {
$(obj).parent().parent().remove();
}
</script>
</head>
<body>
<form id="fileForm" action="" method="post" enctype="multipart/form-data">
<table id="fileTable">
<tr>
<td>
<input type="file" name="file"><input type="button" id="addButon" value="Add" onclick="add()">
</td>
</tr>
</table>
</form>
</body>
</html>
Copy after login

Introducing various ways for JQuery to obtain the value in input type="text"

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JQuery获取文本框的值</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<script src="jquery-1.5.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
//使用id的方式获取
$(document).ready(function(){
//1
$("#button_text1").click(function(){
var result1 = $("#input_text1").val();
alert("result1 = " + result1);
});
//2
$("#button_text2").click(function(){
var result2 = $("input[id=&#39;input_text2&#39;]").val();
alert("result2 = " + result2);
});
//3
$("#button_text3").click(function(){
var result3 = $("input[id=&#39;input_text3&#39;]").attr("value");
alert("result3 = " + result3);
});
//4. 可以通过type的值来获取input中的值(未演示)
/*
$("#button_text4").click(function(){
var result4 = $("input[type=&#39;text&#39;]").val();
alert("result4 = " + result4);
});
*/
//5. 可以通过name的值来获取input中的值(未演示)
/*
$("#button_text5").click(function(){
var result5 = $("input[name=&#39;text&#39;]").val();
alert("result5 = " + result5);
}); 
*/
});
</script>
</head>
<body>
<!-- 获取文本框的值:方式一 -->
<p id="test1">
<input id="input_text1" type="text" value="test1" style="width: 100px;" />
<button id="button_text1">test1</button>
</p>
<!-- 获取文本框的值:方式二 -->
<p id="test2">
<input id="input_text2" type="text" value="test2" style="width: 100px;" />
<button id="button_text2">test2</button>
</p>
<!-- 获取文本框的值:方式三 -->
<p id="test3">
<input id="input_text3" type="text" value="test3" style="width: 100px;" />
<button id="button_text3">test3</button>
</p>
</body>
</html>
Copy after login

More jQuery dynamically adds

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!