提交时打开新窗口

时间:2016-11-06 21:10:51

标签: javascript html

我在html中有一个问题。我已经编写了我的代码,但现在我想在有人点击提交按钮后打开一个新窗口,在这个新窗口中,他或她输入的文本将被显示。

<html>
<body>
<font face="Arial,Helvetica">Hello! 
</body>  
<p><label for="vname">Tell me something:
<input type="text" id="vname" name="vname">
    </font> 
<body>
</p>
</label>
<input type="submit" value="Submit" onclick="myAlert()">
    </html>

3 个答案:

答案 0 :(得分:0)

此代码将让您打开一个新窗口并使用表单值填充

function myAlert() {
    var NewWindow= window.open();
    var nameValue = document.getElementById("vname").value;
    NewWindow.document.write("<p>Some text followed by form value </p>"+  nameValue );
    }

答案 1 :(得分:0)

您缺少一些东西。您需要包含表单并阻止表单发送。这是一个让你走向正确方向的例子!

items = new List<TextCell>();
        items.Add(new TextCell { Text = "Cake", TextColor = Color.Green, Detail = "12 hours left!", DetailColor = Color.Black});
        items.Add(new TextCell { Text = "Pie", TextColor = Color.Green, Detail = "14 hours left!", DetailColor = Color.Black });


        var buy = new ContentPage
        {
            Title = "Buy",
            Content = new StackLayout
            {
                VerticalOptions = LayoutOptions.Center,
                Children = {
                    new ListView
                    {
                       ItemsSource = items,
                       ItemTemplate = new DataTemplate(typeof(TextCell))
                    }
                }
            }
        };
(function(){
	var f, s, w;
	
	f = document.forms.submit;
	
	f.addEventListener("submit", function(e){
		e.preventDefault();
		
		s = "Name: ";
		s += f.name.value;
		s += "<br/>";
		s += "Last Name: ";
		s += f.last.value;
		
		w = window.open();
		w.document.write(s);
	});
}());

答案 2 :(得分:-1)

首先,您应该使用<form>。否则,<input type="submit">并没有真正做任何事情。完成后,请使用window.open()

<form onsubmit="return false;">
    <label for="vname">Tell me something:
    <input type="text" id="vname" name="vname">
    <input type="submit" value="Submit" onclick="myAlert()">
</form>

JavaScript:

function myAlert() {
    var results = window.open();
    var vname = document.getElementById("vname").value;
    results.document.write(vname);
}

基本上,JS获取输入的值,打开一个新页面,然后使用document.write显示结果。

原始答案

(提问者要求我把这个版本的答案备份)
<form onsubmit="myAlert(); return false">
    <label for="vname">Tell me something:
    <input type="text" id="vname" name="vname">
    <input type="submit" value="Submit">
</form>

JavaScript:

function myAlert() {
    var vname = document.getElementById("vname").value;
    document.write(vname);
}