日期:2014-05-18  浏览次数:20422 次

服务器按钮 调用 一段 JS函数 没反应,帮看看是怎么回事??
实现的功能就是在文本框中输入内容 弹出 百度的 查找页面~~
现在的问题是 点击服务器按钮一Button1 没反应??
点击按钮 二 可以调用 百度搜索 但是 如果是中文 会出现乱码??
各位兄弟帮看看~
HTML code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="10_调用Baidu.aspx.cs" Inherits="_10_调用Baidu" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 
Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language="javascript">
function search()
{
    window.location="http://www.baidu.com/s?wd=" + document.getElementById("key").value + "&cl=3";
}
function search2()
{
    window.location="http://www.baidu.com/s?wd=" + document.getElementById("key2").value + "&cl=3";
}
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="key" runat="server"></asp:TextBox>
        &nbsp;
        <asp:Button ID="Button1" runat="server" Text="百度一下" OnClientClick="search()" /><br>
      <input id="key2" type="text" />
        <button onclick="javascript:search2();">百度一下</button>

       
    </div>
    </form>
</body>
</html>



------解决方案--------------------
OnClientClick换成onclick 前面的那个需要有返回值的把...
中文显示乱码 应该是你的编码的问题
------解决方案--------------------
<asp:TextBox>被解析成<input type="submit">,用来提交表单。在还没有执行函数的时候,已经提交表单了,就不会再执行search函数中的内容了

把search函数改成如下:

function search()
{
event.returnValue = false;
window.location.href="http://www.baidu.com/s?wd=" + document.getElementById("key").value + "&cl=3";
}
------解决方案--------------------
--中文乱码是编码的问题,先用escape(str)处理一下参数试一下