[英]Set value of textbox using JQuery
My Jade template -我的翡翠模板 -
input#main_search.span2(
style = 'height: 26px; width: 800px;' ,
type = 'text',
readonly='true',
name='searchBar',
value='test'
)
JS file - JS文件——
$('#searchBar').val('hi')
console.log('sup')
Console output -控制台输出 -
sup
But searchBar
value stats at test.但是searchBar
值统计在测试中。 What am I doing wrong?我究竟做错了什么?
You are logging sup
directly which is a string您正在直接记录sup
这是一个字符串
console.log('sup')
Also you are using the wrong id您也使用了错误的 ID
The template says #main_search
but you are using #searchBar
模板显示#main_search
但您正在使用#searchBar
I suppose you are trying this out我想你正在尝试这个
$(function() {
var sup = $('#main_search').val('hi')
console.log(sup); // sup is a variable here
});
Make sure you have the right selector, and then wait until the page is ready and that the element exists until you run the function.确保您有正确的选择器,然后等到页面准备好并且元素存在,直到您运行该函数。
$(function(){
$('#searchBar').val('hi')
});
As Derek points out, the ID is wrong as well.正如 Derek 指出的那样,ID 也是错误的。
Change to $('#main_search')
更改为$('#main_search')
1) you are calling it wrong way try: 1)你用错误的方式调用它尝试:
$(input[name="searchBar"]).val('hi')
2) if it doesn't work call your .js file at the end of the page or trigger your function on document.ready event 2) 如果它不起作用,请在页面末尾调用您的 .js 文件或在 document.ready 事件上触发您的函数
$(document).ready(function() {
$(input[name="searchBar"]).val('hi');
});
You're targeting the wrong item with that jQuery selector.您使用该 jQuery 选择器定位了错误的项目。 The name
of your search bar is searchBar
, not the id
.搜索栏的name
是searchBar
,而不是id
。 What you want to use is $('#main_search').val('hi')
.您要使用的是$('#main_search').val('hi')
。
$(document).ready(function() {
$('#main_search').val('hi');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.