【问题标题】:Go to the url of the selection after submitting the form提交表单后转到选择的url
【发布时间】:2014-03-17 13:05:42
【问题描述】:

我有以下代码,因此根据所选选项,访问者会在更改时被定向到新页面。但我希望他们在点击提交按钮后被引导到该页面,而不是在更改时立即被引导到该页面。我该怎么做?

<script type="text/javascript"> 
function go()
{
window.location=document.getElementById("link").value
}
</script>
<form>
<select id="link" onchange="go()" > <option>DEFAULT</option>
  <option value="page1.php">TITLE1</option>
  <option value="page2.php">TITLE2</option>
  <option value="page3.php">TITLE3</option>
</select>

<div class="submit">  <input name="submit" type="image" id="submit" src="images/getaquotebutton.png"  height="93" width="259"/> </div><!--end of submit class-->

</form>

在此先感谢

【问题讨论】:

  • 更改为表单上的 onsubmit 事件,而不是 select 上的 onchange。
  • 谢谢 Gordon,我之前尝试过,但由于某种原因没有奏效。

标签: php jquery forms select


【解决方案1】:

这很简单:

从这里移除 onchange:

<select id="link" onchange="go()" >

改为添加:

<input onclick="go()" name="submit" type="image" id="submit" src="images/getaquotebutton.png"  height="93" width="259"/>

编辑:

刚刚注意到type 设置为image,如果这不能按预期工作,请尝试将type 更改为button 并通过css 添加图像为:

<input onclick="go()" name="submit" type="button" id="submit" style="background-image: url('images/getaquotebutton.png'); height:93px; width:259px;" />

【讨论】:

  • 谢谢,但是当我这样做时,网站上没有显示下拉菜单。所以我这样编辑:
    我错过了什么吗?
  • @user3428914 从您的代码中,&lt;select id="link"&gt; 似乎丢失了。另请注意编辑。
  • 只是为了让其他人更容易复制粘贴,我做了最后一个这样的类,因为当 url 和括号之间有空格时,它不显示背景图像. &lt;div class="submit"&gt; &lt;input onclick="go()" name="submit" type="button" id="submit" style="background-image: url('images/getaquotebutton.png'); height:93px; width:259px;" /&gt; &lt;/div&gt;&lt;!--end of submit class--&gt;
  • @Denise 没错,不应该有空格,我也会更新答案。
【解决方案2】:

使用 jQuery,您可以执行以下操作:

$('#submit').click(function(
    window.location=document.getElementById("link").value;
));

当然你应该删除onchange。

【讨论】:

  • 感谢您,尽管此代码在 Dreamweaver 上出现错误。这里可能有什么错字吗?
【解决方案3】:
$(function() {

$('form').on('submit', function() {
    if($('#link').val() == page1.php) { 
        do redirect..
        }
    });
});

每个人都这样做!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-04
    • 2011-01-15
    • 2016-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多