使用Javascript onsearch事件搜索时如何清除textarea值

时间:2015-05-07 13:01:05

标签: javascript jquery html

我正在使用此方法http://www.w3schools.com/jsref/event_onsearch.asp

我想要的是我搜索过的文本留在textarea或者至少回到textarea,而不是因为我点击“输入”按钮而消失。

我理解它会清除文本,因为在链接中它们描述了这样的函数:“onsearch事件发生在用户按下”ENTER“键或单击type =”search“元素中的”x“按钮时“。

所以它就好像我点击了x按钮一样,但是,必须有一种方法可以让文本在那之后回来吗?

这是我目前的代码html代码

 <form> <input type="search" name="search" id="searchid" onsearch="OnSearch(this)"/> </form>

这是我的javasript / jquery

function OnSearch(input) {
    alert("The current value of the search field is " + input.value);
    $("#searchid").val(input.value);
}

现在发生的是它正确警告textarea持有的值,尽管它不会添加textarea值。

编辑:看起来页面重新加载,如何插入页面重新加载后运行的代码?

2 个答案:

答案 0 :(得分:2)

我有另一种选择。由于您无法避免使用明确的功能,因此您可以在每次keypressed全局变量时存储文本,如果按下x,则保留文本框中的值。以下是代码:

<强> DEMO HERE

var text=""; 
function OnSearch(input) {
    if(input.value == "") {
          $("#searchid").val(text);
    }
    else {
         alert("You searched for " + input.value);
    }
}

$(document).on('keyup','#searchid', function (e) {
    text=$(this).val();
    console.log(text);
});

<强>更新

如果您的html位于表单内,则可以执行以下操作:

签入document.ready是否已有文字,如果是,请设置!!

$(document).ready(function()
{
   if(localStorage.getItem("text")!="")
   {
        $("#searchid").val(localStorage.getItem("text"));
   }
});


function OnSearch(input) {
    if(input.value == "") {
          $("#searchid").val(localStorage.getItem("text"));
    }
    else {
         alert("You searched for " + input.value);
    }
}

$(document).on('keyup','#searchid', function (e) {
    localStorage.setItem("text",$(this).val());
});

答案 1 :(得分:1)

我认为这会帮助您显示警告对话框符号:

HTML:

<input type="search" name="search" id="searchid"/>

使用Javascript:

document.getElementById("searchid").onsearch = function() {yourfunctionname()};
/* Put this before the below function or in the top of the document */
function yourfunctionname(){
  var x = document.getElementById("searchid").value;
  alert("The current value of the search field is "+x);
  /* Or do what ever you wish */
}
/*Remember to replace the yourfunctionname with your function's name */

如果是表格,请尝试:

您的表单应如下所示:

<form method="/* method */" action="/* action */" onSubmit="yourfunctionname()">
  <input type="search" name="search" id="searchid"/>
  /* Rest of your form*/
</form>

使用Javascript:

document.getElementById("searchid").value = localStorage.getItem("saved");
document.getElementById("searchid").onsearch = function() {yourfunctionname()};
/* Put this before the below function or in the top of the document */
function yourfunctionname(){
  var x = document.getElementById("searchid").value;
  alert("The current value of the search field is "+x);
  /* Or do what ever you wish */
  /* The below code does the trick*/
  localStorage.setItem("saved", x);
  location.reload();
  return false;     
}
/* Remember to replace the yourfunctionname with your function's name */

如果您有不同的功能来提交表单,那么将该表单的onsubmit属性替换为该函数的名称,并在其名称前添加单词“return”,并在该函数中添加以下javascript。

var x = document.getElementById("searchid").value
localStorage.setItem("saved", x);
location.reload();
return false;

如果您想要其他内容,请发表评论。

请接受,好像它可以解决您的问题。 谢谢......

最后还有更多的评论

相关问题