简体   繁体   English

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

[英]pass javascript variables to form/input fields as onsubmit href link

id like to have an input box that a user can enter a search term into that gets passed to maybe a javascript function that then combines some url segments with the search term creating a full url. id喜欢有一个输入框,用户可以输入一个搜索词,然后传递给一个javascript函数,然后将一些url段与搜索词组合在一起创建一个完整的url。 up until now its been working fine without a form around it, but i would like to add a form to it so users can just hit enter rather than clicking a submit button. 到目前为止它一直工作正常,没有周围的形式,但我想添加一个表单,以便用户只需按Enter键而不是单击提交按钮。

submit button code: 提交按钮代码:

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

faulty form/input code: 错误的表格/输入代码:

<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>

possible js function to create url: 可能的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>

Use form's onSubmit event where set the appropriate data to the intranet's input - don't change this.href (form does not have such attribute). 使用表单的onSubmit事件,在其中设置适当的数据到Intranet的输入 - 不要更改this.href(表单没有这样的属性)。 Remember though that JavaScript can be disabled at the client's side. 请记住,可以在客户端禁用JavaScript。 Consider also placing hidden inputs into the form for i1 and i2 and do all combination logics at the server side. 考虑将隐藏的输入放入i1和i2的表单中,并在服务器端执行所有组合逻辑。

<form action="" onSubmit="urlGen()">
...
</form>

<script language="javascript" type="text/javascript">
                    function urlGen()
                        {


                            var i1 = "urlSeg1";
                            var i2 = document.getElementById('intranet').value;
                            var i3 = "urlSef2";

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


                        }
                </script>

Maybe something like: 也许是这样的:

<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;
}

should also note that this is for a sidebar gadget and just needs to work with ie and server/client limitations is null. 还应该注意,这是一个侧边栏小工具,只需要使用ie和服务器/客户端限制为空。 tried both of those methods (i think - still a beginner), but all that happens when i hit enter is the page reloads (target="_blank" opens new page of the same). 尝试了这两种方法(我认为 - 仍然是初学者),但当我点击输入时发生的所有事情都是页面重新加载(target =“_ blank”打开相同的新页面)。

id like to do away with the submit button area entirely, but it is functional with that and without the form tags. 我想完全取消提交按钮区域,但它具有功能,没有表单标签。

what i have right now: 我现在拥有的:

<div id="row2">

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

                            var fullURL = i1 + i2 + i3;
                            f.action = i1 + i2 + i3;

                            return true;
                        }
                </script>

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

            <br><br>

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM