怎么在jquery里清空文本框的内容?
在jquery中,通过获得文本框对象,使用val()方法设置内容为空,便可以实现清空文本框的内容。下面小编以input文本框为例,讲解怎么在jquery里清空文本框的内容。
1.新建一个html文件,命名为test.html,用于讲解怎么在jquery里清空文本框的内容。
2.在test.html文件内,在p标签内,使用input标签创建一个文本框,代码如下:
3.在test.html文件内,设置input文本框的id为myinput,主要用于下面通过该id获得input对象。
4.在test.html文件内,使用button标签创建一个按钮,按钮名称为“清空文本框内容”。
5.在test.html文件中,给button按钮绑定onclick点击事件,当按钮被点击时,执行myfun()函数。
6.在js标签中,创建myfun()函数,在函数内,通过id(myinput)来获得input文本框对象,使用val()方法设置为空内容,从而实现清除文本框的内容。
7.在浏览器打开test.html文件,在文本框输入内容,点击按钮,查看实现的效果。
jquery中的submit方法怎么用?
submit()方法绑定在form表单元素上,当绑定的表单被提交时(通过点击按钮、按回车键等),会触发该方法。实例:
<form id="form1" autocomplete="off"> <input type="text" name="model.username"/> <input type="password" name="model.password"/> <input type="submit" value="登录" /> </form>
$('#form1').submit(function() { $.ajax({ url: '/index!login.do', data: $('#form1').serialize(), type: "POST", dataType: "json", cache: false, success: function(data) { if (data.login == true || data.login == "true") { // 登录成功 location.replace('/main/index.do'); } else { alert('登录失败!请检查用户名或密码'); } } });});
jQuery通用的全局遍历方法$.each()用法实例?
1.test.json文件代码:
[
{
"username": "张三",
"content": "沙发."
},
{
"username": "李四",
"content": "板凳."
},
{
"username": "王五",
"content": "地板."
}
]
2.html代码:
<p>
<input type="button" id="send" value="加载"/>
</p >
<div >已有评论:</div>
<div id="resText" ></div>
3.jQuery代码:
<script src="jquery-1.3.1.js" type="text/javascript"></script>
<script type="text/javascript">
/*
1.$.each()是jquery的一个通用的遍历方法,可用于遍历对象和数组
2.$.each()函数不同于jquery对象的each()方法,它是一个全局函数,不操作jquery对象,而是以一个数组或者对象作为第一个参数,以一个回调函数作为第二个参数。回调函数拥有两个参数:第一个参数为对象的成员或数组的索引,第二个参数为对应变量或内容
*/
$(function(){
$('#send').click(function() {
$.getJSON('test.json', function(data) {
$('#resText').empty();
var html = '';
$.each( data , function(commentIndex, comment) {
html += '<div ><h6>' + comment['username'] + ':</h6><p >' + comment['content'] + '</p ></div>';
})
$('#resText').html(html);
})
})
})
</script>

