编程语言
首页 > 编程语言> > 将javascript变量传递给表单/输入字段作为onsubmit href链接

将javascript变量传递给表单/输入字段作为onsubmit href链接

作者:互联网

id喜欢有一个输入框,用户可以输入一个搜索词,然后传递给一个javascript函数,然后将一些url段与搜索词组合在一起创建一个完整的url.到目前为止它一直工作正常,没有周围的形式,但我想添加一个表单,以便用户只需按Enter键而不是单击提交按钮.

提交按钮代码:

<a href="javascript: void(0)" onClick="this.href = i1 + document.getElementById('intranet').value + i3" target="_blank">
                    <div id="submit" class="out"
                        onm ouseOver="document.getElementById('submit').className = 'over';"
                        onm ouseOut="document.getElementById('submit').className = 'out';">
                        <span>Submit</span>
                    </div>
                </a>

错误的表格/输入代码:

<form action="" onSubmit="this.href = i1 + document.getElementById('intranet').value + i3; return false;" method="get" target="_blank">
                <input type="text" id="intranet" size="15" value="Search Intranet.."
                    onFocus="document.getElementById('intranet').value = ''"
                    onBlur="document.getElementById('intranet').value = 'Search Intranet..'" / >    
                </form>

可能的js函数来创建url:

<script language="javascript" type="text/javascript">
                    function urlGen(value)
                        {
                            var i1 = "seg1";
                            var i2 = document.getElementById('intranet').value;
                            var i3 = "seg2";

                            var fullURL = i1 + document.getElementById('intranet').value + i3;

                            return fullURL;
                        }
                </script>

解决方法:

也许是这样的:

<form action="" onSubmit="urlGen(this);" method="get" target="_blank">
    <input type="text" id="intranet" size="15" value="Search Intranet.."
                 onFocus="this.value = ''"
                 onBlur="this.value = 'Search Intranet..'" / >    
</form>

function urlGen(f){
   var i1 = 'urlSeg1';
   var i2 = f.intranet.value;
   var i3 = 'urlSef';
   f.action = i1 + i2 + i3;
   return true;
}

标签:onsubmit,javascript,forms,input,href
来源: https://codeday.me/bug/20190827/1739959.html