[英]button onclick call javascript function not working
Im trying to call a function when user clicks on the navbar search button but it does not seem to call the javascript fuction and just loads the same page. 我试图在用户点击导航栏搜索按钮时调用一个函数,但它似乎没有调用javascript函数,只是加载相同的页面。 I have looked at examples and other questions but I dont see anyhing wrong.. Any help would be much appreciated, Here is my code, It is in my Masterpage.aspx:
我看过例子和其他问题,但我没有看到任何错误..任何帮助将不胜感激,这是我的代码,它在我的Masterpage.aspx:
<div class='navbar-form navbar-left' role='search'>
<div class='inputgroup'>
<input class='form-control' id='navinput' type='text' placeholder='Search'/>
<button class='btn btn-default' type='submit' id='navsearchbtn' runat='server' onclick='NavToSearch();'>
<span class='glyphicon glyphicon-search'></span>
</button>
</div>
</div>
Here is my Js code (also in Masterpage.aspx): I just have the test redirect to see if it works. 这是我的Js代码(也在Masterpage.aspx中):我只是测试重定向以查看它是否有效。 (The code that i want to implement is in comments please check that is okay as well, I then want to use the input text to search database)
(我要实现的代码在评论中请检查一下也没问题,然后我想使用输入文本来搜索数据库)
<script type='text/javascript'>
function NavToSearch() {
window.location.href = 'Search.aspx';
/*var navsearchText = $('$navinput').text();
if (navsearchText == '')
{
$('$navinput').attr('placeholder', 'Enter Search Text');
return false;
}
else window.location.href = 'Search.aspx'; */
}
</script>
UPDATED script/function: 更新的脚本/功能:
<script type='text/javascript'>
function NavToSearch()
{
var navsearchText = $('#navinput').text();
if (navsearchText == '')
{
$('#navinput').attr('placeholder', 'Enter Search Text');
return false;
}
else {
window.location.href = 'Search.aspx';
//return false;
}
/*OR
$(document).ready(function(){
var url = "OrderHistory.aspx";
$(location).attr('href',url);
})*/
}
</script>
If youwant just to call the NavToSearch()
when the button clicked you have to change the type of the button from submit
to button
: 如果您只想在单击按钮时调用
NavToSearch()
,则必须将按钮类型从submit
更改为button
:
<button class='btn btn-default' type='button' id='navsearchbtn' runat='server' onclick='NavToSearch();'>
<span class='glyphicon glyphicon-search'></span>
</button>
In your commented code you have to replace $
sign by #
for the id navinput
and also .text()
by .val()
: 在您的注释代码中,您必须使用
#
替换id为navinput
$
sign,并将.text()
替换$
.val()
:
$('#navinput').val();
Instead of : 代替 :
$('$navinput').text();
Hope this helps. 希望这可以帮助。
Sounds like you are using Asp.net Web Forms, if true, your page will have a form
element, then for a button
with type=submit
will trigger the form submit by default, generally this will redirect to your current page. 听起来像你使用的是Asp.net Web Forms,如果是,你的页面将有一个
form
元素,然后对于一个type=submit
的button
,默认情况下会触发表单提交,通常会重定向到你当前的页面。
You can avoid this by return false
in the click handler: 您可以通过在单击处理程序中
return false
来避免这种情况:
function NavToSearch() {
window.location.href = 'Search.aspx';
/*var navsearchText = $('$navinput').text();
if (navsearchText == '')
{
$('$navinput').attr('placeholder', 'Enter Search Text');
}
else window.location.href = 'Search.aspx'; */
return false; //prevent the form submit whatever.
}
For a submit button, either the form submission will cancel the redirect, or the redirect will cancel the submission if return false
is added after. 对于提交按钮,表单提交将取消重定向,或者如果之后添加了
return false
,则重定向将取消提交。
Check this question: 检查这个问题:
How do I redirect users after submit button click? 如何在提交按钮单击后重定向用户?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.