Javascript 清除文本框值并重置所有值
对于以下代码:Javascript 清除文本框值并重置所有值,javascript,jquery,html,Javascript,Jquery,Html,对于以下代码: $(document).ready(function () { $('#error_msg').hide(); }); //When user clicks enter button function clickenter() { text = document.getElementById('text_one').value; if (text == "" || text == null || te
$(document).ready(function () {
$('#error_msg').hide();
});
//When user clicks enter button
function clickenter() {
text = document.getElementById('text_one').value;
if (text == "" || text == null || text.length > 50) {
$('#error_msg').show('slow');
return false;
} else {
$('#list').append('<li>' + text + '</li><br />');
text == "";
}
};
//When user presses Enter
document.getElementById('text_one').onkeypress = function (e) {
if (!e) e = window.event;
var keyCode = e.keyCode || e.which;
if (keyCode == '13') {
text = document.getElementById('text_one').value;
if (text == "" || text === null || text.length > 50) {
$('#error_msg').show('slow');
return false;
} else {
$('#list').append('<li>' + text + '</li><br />');
text == "";
}
}
};
$('#error_mssg_x').click(function () {
$('#error_msg').hide('slow');
});
$(文档).ready(函数(){
$('#error_msg').hide();
});
//当用户单击enter按钮时
函数clickenter(){
text=document.getElementById('text_one')。值;
如果(text==“”| | text==null | | text.length>50){
$('error_msg')。show('slow');
返回false;
}否则{
$(“#列表”).append(“”+text+”
);
文本==“”;
}
};
//当用户按Enter键时
document.getElementById('text_one')。onkeypress=function(e){
如果(!e)e=window.event;
var keyCode=e.keyCode | | e.which;
如果(键代码=='13'){
text=document.getElementById('text_one')。值;
如果(text==“”| | text==null | | text.length>50){
$('error_msg')。show('slow');
返回false;
}否则{
$(“#列表”).append(“”+text+”
);
文本==“”;
}
}
};
$('#错误_mssg_x')。单击(函数(){
$('#error_msg')。隐藏('slow');
});
当函数运行时,如何将文本的值设置为零?我环顾四周,发现将它的值设置为“”会起作用,但似乎不起作用
我还想添加一个“清除”按钮,但我不太确定我将如何做到这一点。所有项目都会附加到这些HTML元素中:
<div id="title_box">
<ul id="list"></ul>
</div>
关于文本==”代码>
尝试只使用一个等号=
您应该分配text=”“代码>
对于重置#list
id中的值,也可以使用
document.getElementById('#list').innerHTML = "";
这将清除列表中的所有附加值。将一些示例放在一起。您可能希望使用jquery选择器访问输入元素,在这里可以使用val()方法。设置.val(“”)将文本设置为“空”。这里有一些示例可以帮助您入门(尽可能重复使用一些名称)
document.getElementById('text_one').onkeypress = function (e) {
if (!e) e = window.event;
var keyCode = e.keyCode || e.which;
if (keyCode == '13') {
text = document.getElementById('text_one').value;
if (text == "" || text === null || text.length > 50) {
$('#error_msg').show('slow');
return false;
} else {
$('#list').append('<li>' + text + '</li><br />');
document.getElementById('text_one').value = "";
}
}
};
你也注意到在你的代码中你有同样的代码做同样的事情?“if(text=”“| |……)。我将其合并为一个函数,可以通过按键或按钮单击来调用。最后放一把小提琴
加价
<div>
<input type="text" id="text_one" placeholder="Enter Value Here" />
<input type="button" id="btnclick" value="Add" />
<input type="button" id="btnclear" value="Clear List" />
</div>
<div id="ErrorMessage" style="display: none;">
You entered an invalid value <span id="DismissError" style="cursor: default;">×</span>
</div>
<div>
<ul id="listItems"></ul>
</div>
您输入了无效的值×;
使用jQuery的JavaScript
$(function(){
$("#btnclick").click(function(){
enterValue();
});
$("#text_one").keypress(function(e){
if ( e.which == 13 ) {
enterValue();
}
});
$("#DismissError").click(function(){
hideError();
});
$("#btnclear").click(function(){
clearList();
});
});
function enterValue(){
hideError();
var value = $("#text_one").val();
if(!value || value.length > 50){
return doError();
}
$("#listItems").append("<li>" + value + "</li>");
$("#text_one").val("");
}
function doError(){
$("#ErrorMessage").show();
}
function hideError(){
$("#ErrorMessage").hide();
}
function clearList(){
$("#listItems").html("");
}
$(函数(){
$(“#btnclick”)。单击(函数(){
enterValue();
});
$(“#text_one”)。按键(功能(e){
如果(e.which==13){
enterValue();
}
});
$(“#DismissError”)。单击(函数(){
hideError();
});
$(“#btnclear”)。单击(函数(){
clearList();
});
});
函数enterValue(){
hideError();
var值=$(“#text_one”).val();
如果(!value | | value.length>50){
返回doError();
}
$(“#列表项”)。追加(“”+value+“ ”);
$(“#text_one”).val(“”);
}
函数doError(){
$(“#ErrorMessage”).show();
}
函数hideError(){
$(“#ErrorMessage”).hide();
}
函数clearList(){
$(“#列表项”).html(“”);
}
JSFIDLE
text\u one是用户输入信息的文本框的id
$(function(){
$("#btnclick").click(function(){
enterValue();
});
$("#text_one").keypress(function(e){
if ( e.which == 13 ) {
enterValue();
}
});
$("#DismissError").click(function(){
hideError();
});
$("#btnclear").click(function(){
clearList();
});
});
function enterValue(){
hideError();
var value = $("#text_one").val();
if(!value || value.length > 50){
return doError();
}
$("#listItems").append("<li>" + value + "</li>");
$("#text_one").val("");
}
function doError(){
$("#ErrorMessage").show();
}
function hideError(){
$("#ErrorMessage").hide();
}
function clearList(){
$("#listItems").html("");
}