jquery 如何从搜索框中获取字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11716126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery How to get string from the search box
提问by Dc Redwing
I am studying jquery searching box.
我正在研究 jquery 搜索框。
I am getting trouble with getting string var from html.
我在从 html 获取字符串 var 时遇到了麻烦。
in my html file.
在我的 html 文件中。
i have..
我有..
<header>
<div id="searchBox">
<form name="searchForm">
<div>
<i class="icon-search icon-large icon-height"></i>
<input name="searchfield" type="search" size="20" maxlength="30">
</div>
</form>
</div>
</header>
and
和
in my jquery file i have
在我的 jquery 文件中,我有
$(".icon-search").click(function() {
var searchstring = $(this).text('#searchfield');// $('#searchfield');
searchstring.focus();
//$('#searchBox').html($('input:searchfield').val());
alert("searchfield. " + searchstring + " hahah");
});
when I clicked to searching some strings.
当我点击搜索一些字符串时。
It shows that Object object instead of some strings.
它显示的是 Object 对象而不是一些字符串。
I am not really sure how to convert object to strings.
我不太确定如何将对象转换为字符串。
Does anybody know how to convert it ?
有人知道如何转换吗?
回答by dxh
This line of code means nothing:
这行代码没有任何意义:
var searchstring = $(this).text('#searchfield');// $('#searchfield');
It should indeed be:
确实应该是:
var searchstring = $('#searchfield');
Then you'll be able to do:
然后你将能够做到:
searchstring.focus();
And retreive the text with .val()
:
并使用以下命令检索文本.val()
:
alert("searchfield. " + searchstring.val() + " hahah");
.text()
gets or sets the text content of a HTML element. It getsa value if it is being invoked without any parameters, and it setsa value if a value is passed to it. So $(this).text('#searchfield')
would set the inner text of $(this)
(your .icon-search
) to the string "#searchfield"
.
.text()
获取或设置 HTML 元素的文本内容。如果它在没有任何参数的情况下被调用,它就会得到一个值,如果一个值被传递给它,它就会设置一个值。所以$(this).text('#searchfield')
会将$(this)
(your .icon-search
)的内部文本设置为 string "#searchfield"
。
Now, a form element doesn't have an inner text, it has a value, hence .val()
, which otherwise works the same way as .text()
现在,表单元素没有内部文本,它有一个value,因此.val()
,它的工作方式与.text()
alert(myTextbox.val()); // get value
myTextbox.val('test'); // set value
Edit:
编辑:
Also, $('#searchfield')
looks for an element of the ID "searchfield". You should either add id="searchfield"
to your textfield, or update your script to
此外,$('#searchfield')
查找 ID 为“searchfield”的元素。您应该添加id="searchfield"
到文本字段,或将脚本更新为
var searchstring = $('input[name=searchfield]');
回答by afrin216
Your code access the DOM object.
您的代码访问 DOM 对象。
Try $("#searchfield").attr('value')
尝试 $("#searchfield").attr('value')
You can put any attribute you want in the attr() function and get it. To get its id just use
$("#searchfield").attr('id')
您可以在 attr() 函数中放入您想要的任何属性并获取它。要获得它的 id 只需使用
$("#searchfield").attr('id')
Personally I use this a lot than val()
, as it causes cross browser problems with some other DOM objects.
就我个人而言,我经常使用val()
它,因为它会导致其他一些 DOM 对象的跨浏览器问题。