我有这段代码:
如何让我的代码与两个选项一起使用?因为现在它只使用一个选择,因为我认为代码不是正确的重复
<form action="https://track.aftership.com/tuffnells/" method="get" target="_blank" id="my-form">
<input type="text" name="reference-number" id="reference-number" value="" />
<input type="submit" value="submit" />
</form>
<script type="text/javascript">
var form = document.querySelector('#my-form'),
text_field = document.querySelector('#reference-number');
function submitHandler(){
// build the new url and open a new window
var url = form.action + text_field.value + '?tracking_account_number=318843';
window.open(url);
// prevent form from being submitted because we already
// called the request in a new window
return false;
}
// attach custom submit handler
form.onsubmit = submitHandler;
</script>
<form action="https://track.aftership.com/tnt-reference/" method="get" target="_blank" id="my-form1">
<input type="text" name="reference-number1" id="reference-number1" value="" />
<input type="submit" value="submit" />
</form>
<script type="text/javascript">
var form = document.querySelector('#my-form1'),
text_field = document.querySelector('#reference-number1');
function submitHandler(){
// build the new url and open a new window
var url = form.action + text_field.value + '?';
window.open(url);
// prevent form from being submitted because we already
// called the request in a new window
return false;
}
// attach custom submit handler
form.onsubmit = submitHandler;
</script>
我怎样才能完成这项工作两次,因为现在只能工作一次。
答案 0 :(得分:0)
只需在第二个脚本部分重命名变量,即:
var form = document.querySelector('#my-form1'),
text_field = document.querySelector('#reference-number1');
为:
var form1 = document.querySelector('#my-form1'),
text_field1 = document.querySelector('#reference-number1');
也改变了(在第二个脚本上):
var url = form.action + text_field.value + '?';
window.open(url);
为:
var url1 = form1.action + text_field1.value + '?';
window.open(url1);
这是一个小提琴:https://jsfiddle.net/cLhvuy68/