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

发布于 2024-09-11 12:02:15 字数 1712 浏览 10 评论 0原文

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"
                        onMouseOver="document.getElementById('submit').className = 'over';"
                        onMouseOut="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>

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

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:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

池木 2024-09-18 12:02:15

也许是这样的:

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

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;
}
深者入戏 2024-09-18 12:02:15

使用表单的 onSubmit 事件将适当的数据设置为 Intranet 的输入 - 不要更改 this.href (表单没有此类属性)。请记住,JavaScript 可以在客户端禁用。还可以考虑将隐藏输入放入 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>

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). Remember though that JavaScript can be disabled at the client's side. Consider also placing hidden inputs into the form for i1 and i2 and do all combination logics at the server side.

<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>
能否归途做我良人 2024-09-18 12:02:15

还应该注意,这是一个侧边栏小工具,只需要与 ie 一起使用,并且服务器/客户端限制为空。尝试了这两种方法(我认为 - 仍然是初学者),但是当我按回车键时发生的所有事情都是页面重新加载(target =“_blank”打开相同的新页面)。

我喜欢完全取消提交按钮区域,但它可以在没有表单标签的情况下发挥作用。

我现在拥有的:

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

should also note that this is for a sidebar gadget and just needs to work with ie and server/client limitations is null. 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).

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文