我已经用多个按钮编写代码,每个按钮都有不同的值。 我想在javascript函数中获取该值。使用Javascript函数获得输入值
<form action="https://www.bing.com" method="get">
<input class="input" type="text" name="q" id="field" autofocus>
<div class="buttons">
<input type="submit" class="button" id="x1" name="x1" value="Bing">
<input type="submit" class="button" id="x2" name="x2" value="Google">
<input type="submit" class="button" id="x3" name="x3" value="Yahoo">
<input type="submit" class="button" id="x4" name="x4" value="Duck">
<input type="submit" class="button" id="x5" name="x5" value="Ask">
<input type="submit" class="button" id="x6" name="x6" value="Aol.">
</div>
</form>
</div>
<script>
var form = document.querySelector('form');
var input = document.getElementById('field');
var x = document.getElementById('x1').value;
form.addEventListener('submit', function(ev) {
ev.preventDefault();
if(x=="Bing") {
var redirect = 'https://google.com/search?q=' + input.value; }
window.location = redirect;
});
</script>
这里输入提交设置值只有当它被点击右?如果我尝试将所有提交按钮标识为xx,那么我可以得到在该功能中单击的按钮。一旦点击相应的提交按钮,我想执行不同的重定向。 (多提交操作: 这个问题不重复,它是特定于JS,这种情况)
是什么'xx'?一些控制你错过了......或者......这是一个错字? – deostroll
此链接http://www.javascript-coder。com/html-form/html-form-submit.phtml可以帮助你。总之它说:if($ _ REQUEST ['Operation'] =='Update') { //在此处更新.. } else if($ _ REQUEST ['Operation'] ==“Insert”) { //在此处插入 } –
@deostroll我写道,说xx可以是x1,x2 ...之间的任何错误方式。 – Novak007