这可能吗?
我有输入框和提交按钮。
指定网址为:www.mywebsite.com/ 点击提交按钮后,通过javascript打开的网址是:www.mywebsite.com/print/hello123 /
答案 0 :(得分:6)
查看演示:http://jsfiddle.net/Gv5bq/
HTML:
<input type="text" id="text" />
<input type="button" id="btn" value="Submit" />
jQuery的:
$("#btn").click( function() {
var url = "http://www.mywebsite.com/print/" + $("#text").val();
window.open(url);
});
更新:(简单JS版) http://jsfiddle.net/Gv5bq/1/
<input type="text" id="text" />
<input type="button" id="btn" value="Submit" onClick="javascript: window.open('http://www.mywebsite.com/print/' + document.getElementById('text').value);" />
答案 1 :(得分:0)
如果您不想使用jQuery,那么这是一种纯粹的js方法。
定义你的html格式:
<form action="http://www.mywebsite.com/" 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 + 'print/' + 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>
答案 2 :(得分:0)
<input type="text" value="" id="id"/>
<button type="button" id="go">GO</button>
$('#go').click(function(){
var id=$('#id').val();
var url="http://www.mywebsite.com/hello";
var new_url= url+id;
window.open(new_url);
});