我希望在我的网页中嵌入搜索器。支持google和百度。我写了如下片段:         <form method="get" id="form2" action="">
        关键字:<input id="Text1" type="text" name="q" />
        <input id="txtinput" type="submit" value=" ..搜索.." onclick="search()"/>
        <input id="Radio1" name="seach" type="radio" />百度&nbsp;&nbsp;&nbsp; 
        <input id="Radio2" name="seach" type="radio" checked="checked" />google
        </form>
        function search()
        {
            var form = document.getElementById('form2');
            var rad1 = document.getElementById('Radio1');
            var rad2 = document.getElementById('Radio2');
            form.actoin = (rad1.checked=="true") ? "http://www.baidu.com/s":"http://www.google.com/search";
            var input = document.getElementById('txtinput');
            input.name = (rad1.checked=="true")? "wd":"q";
        }
因为google的搜索页面是www.google.com/search 文本框的name必须是q 而百度的搜索页面是www.baidu.com/s 文本框的name必须是wd所以我使用js修改属性。 
但是好像var form = document.getElementById('form2');这句话获取不到表单。我通过断点观察到这个form变量获取到的是个4个object对象的数组。所以我 
没办法修改它的action的值。请问这个问题如何解决?

解决方案 »

  1.   

    <SCRIPT LANGUAGE="JavaScript">
    <!--
    function search()
    {
    var txt = document.getElementById('Text1');
    var rad1 = document.getElementById('Radio1');
    var rad2 = document.getElementById('Radio2');
    if(rad1.checked)
    {
    window.location.href='http://www.baidu.com/s?wd='+txt.value;
    }
    if(rad2.checked)
    {
    window.location.href='http://www.google.cn/search?hl=zh-CN&q='+txt.value+'&btnG=Google+%E6%90%9C%E7%B4%A2&meta=&aq=f&oq=';
    }
    }
    //-->
    </SCRIPT>
    <form method="get" id="form2" name="form2" action="">
    关键字:<input id="Text1" type="text" name="q" />
    <input id="txtinput" type="button" value="搜索" onclick="search()"/>
    <input id="Radio1" name="seach" type="radio" />百度&nbsp;&nbsp;&nbsp; 
    <input id="Radio2" name="seach" type="radio" checked="checked" />google
    </form>