如何从选项值转到“链接”按钮?

时间:2016-08-25 12:22:47

标签: javascript html button

如何在更改选项值后点击“链接”按钮并单击“总结”? 我有问题,因为当我改变价值时,它会在我点击提交

之前进入“链接”
<form action="search-form" method="post">
<select onChange="window.location.href=this.value" >
<option value="here comes a link nr 1">coffie medium</option>
<option value="here comes a link nr 2">coffe big</option>
</select>
<button type="submit" name="submit" value="submit"><img src="xyz.JPG"    width="240" height="72"> 
</button>
</form>             

5 个答案:

答案 0 :(得分:1)

onchange元素中删除select并为您的提交按钮编写一个监听器(您已使用search-form操作)

<form action="search-form" method="post">
<select>
<option value="here comes a link nr 1">coffie medium</option>
<option value="here comes a link nr 2">coffe big</option>
</select>
<button type="submit" name="submit" value="submit"><img src="xyz.JPG"    width="240" height="72"> 
</button>
</form> 

修改: @Peril按要求添加解决方案。现在,如果您使用onsubmit表格提交,则可以使用以下内容。请注意,您应该为option

提供有效链接

function submit_form() {
    window.location.href= document.getElementById('select-box').value;
}
<form onsubmit="submit_form()" method="post">
        <select id="select-box">
        <option value="http://google.com">coffie medium</option>
        <option value="http://yahoo.com">coffe big</option>
        </select>
        <button type="submit" name="submit" value="submit">submit</button>
    </form>

编辑2 : @Peril通常,当我们点击anchor标记时,我们会将外部链接加载到页面中。如果您使用window.location.href加载,则会出现cross-origin access错误。将以下代码段放入本地html文件并查看。

// load href initially
document.getElementById('form-button').setAttribute('action', document.getElementById('select-box').value);
// load href on value change of select
function onChangeValue() {
  document.getElementById('form-button').setAttribute('action', document.getElementById('select-box').value);
  return false;
}
<body>
  <select id="select-box" onchange="onChangeValue()">
    <option value="http://www.google.com">coffie medium</option>
    <option value="http://www.facebook.com">coffe big</option>
  </select>
  <form action="#" id="form-button" style="display:inline">
    <input type="submit" value="Submit" />
  </form>
</body>

答案 1 :(得分:1)

尝试在您选择的属性&#39; onchange&#39;并在选项的值中链接,如何:

window.onLoad()

答案 2 :(得分:0)

从选择框中删除onchange事件,并在按钮单击时在js中验证它。如果页面重定向提交按钮的用途

答案 3 :(得分:0)

<form action="search-form" method="post">
<select>
<option value="here comes a link nr 1">coffie medium</option>
<option value="here comes a link nr 2">coffe big</option>
</select>
<button type="submit" name="submit" value="submit"><img src="xyz.JPG"    width="240" height="72"> 
</button>
</form>  

这不起作用,它只是adres url“search-form”

的广告

答案 4 :(得分:0)

好的,如果您使用JavaScript或Jquery?

首先创建了选择:

<select name="form" id="form">
<option value="url">Home</option>
<option value="url">Contact</option>
<option value="url">Sitemap</option>
</select>

尝试.change以获取新位置:

$("#form").change(function(){
var url = $(this).val();
location.assign(url);
});