jQuery val()indexOf不起作用 [英] jQuery val() indexOf not working
本文介绍了jQuery val()indexOf不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果输入值包含某个单词,则尝试执行某些操作,但这不起作用:
Trying to do something if the input value contains a certain word but this doesn't work:
var formMain = $('#MainForm :input[name="Search"]');
if ((formMain).value.indexOf('word') >= 0) {
alert('HAS THIS WORD IN IT!');
示例表格:
<form onsubmit="return OnSubmitSearchForm(event, this);" action="/searchresults.asp" id="MainForm" name="MainForm" method="post" class="search_results_section">
<input type="hidden" value="word" name="Search">
<input type="hidden" value="" name="Cat">
</form>
推荐答案
您的标题中提到了val()
,但实际上并没有使用它. formMain
是一个jquery对象,没有value
,您应该使用val()
.
Your title mentions val()
but you're not actually using it. formMain
is a jquery object and has no value
, you should use val()
.
var formMain = $('#MainForm :input[name="Search"]');
if (formMain.val().indexOf('word') >= 0) {
alert('HAS THIS WORD IN IT!');
}
还要注意,formMain
是一个误导变量名,因为它不是主要形式,而是包含搜索输入的jquery对象.
Also note that formMain
is a misleading variable name as it is not the main form but rather a jquery object which contains the search input.
这篇关于jQuery val()indexOf不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文