jQuery val() indexOf 不起作用
如果输入值包含某个单词,则尝试执行某些操作,但这不起作用:
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>
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!');
Example Form:
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的标题提到了
val()
但您实际上并未使用它。formMain
是一个 jquery 对象,没有value
,您应该使用val()
。另请注意,
formMain
是一个误导性的变量名称,因为它不是主表单,而是包含搜索输入的 jquery 对象。Your title mentions
val()
but you're not actually using it.formMain
is a jquery object and has novalue
, you should useval()
.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.可能你在选择器上犯了错误,应该只使用 .val 和选择器....这里有一些东西供你检查 http://jsfiddle.net/sahil20grover1988/V2dsn/3/
possibly you are doing mistake with selector and should use .val with selector only .... here is something for you check this http://jsfiddle.net/sahil20grover1988/V2dsn/3/