Javascript HTML javascript从输入中获取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10577312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTML javascript get value from input
提问by user127886
How can I use the value of a form field in a confirmation dialog box that is displayed upon submit, as seen below?
如何在提交时显示的确认对话框中使用表单字段的值,如下所示?
<form action='removefinish.php' method='post' "accept-charset='UTF-8'">
Role: <br>
<select name="role">
<option value="" selected="selected"></option>
<option VALUE="Administrator"> Administrator</option>
<option VALUE="Employee"> Employee</option>
</select>
<br>
<label for='username'>ID: </label>
<br>
<input type='text' name='username' id='username'/>
<br>
<br><br>
<input type='submit' name='Submit' value='Submit' onclick="return confirm('Are you sure to remove ID: ')";/>
回答by Jukka K. Korpela
Replace the onclick
attribute by the following:
将onclick
属性替换为以下内容:
onclick="return confirm('Are you sure to remove ID ' +
document.getElementById('username').value + '?')"
回答by Nimphious
You need to hook into the form's submit event.
您需要挂钩表单的提交事件。
In your case, you need to give your form
element an ID, in this example we give it the id formID
在你的情况下,你需要给你的form
元素一个 ID,在这个例子中我们给它 idformID
<form id="formID" action="removefinish.php" method="post" "accept-charset='UTF-8'">
Then, you hook into the form's event like this:
然后,您可以像这样挂钩表单的事件:
function formHook() {
document.getElementById('formID').addEventListener('submit', function(e) {
// This will fire when the form submit button is pressed,
// or when the user presses enter inside of a field.
// Get the value from the username field.
var value = document.getElementByID('username').value;
// Ask the user to confirm.
if (!confirm('Are you sure you wish to remove ' + value + ' from the database?')) {
// Stop the form from processing the request.
e.preventDefault();
return false;
}
}, false);
}
You need to do this after the document has loaded, to make sure javascript can find the form, rather than look before it's been added to the DOM. To do this, you can simply use something like this:
您需要在文档加载后执行此操作,以确保 javascript 可以找到表单,而不是在将其添加到 DOM 之前查看。要做到这一点,你可以简单地使用这样的东西:
window.addEventListener('load', formHook, false);
And now, when the document has finished loading, the formHook
function will be called.
现在,当文档完成加载时,formHook
将调用该函数。
回答by webdevkit
You could return confirm('Are you sure to remove ID: ' + document.getElementById('username').value() ) );"
你可以 return confirm('Are you sure to remove ID: ' + document.getElementById('username').value() ) );"
However, you would probably want to make a function out of that and just return customConfirmFunction( );
to make it easier to read and future-proof it, in case you need to add some more logic to it.
但是,您可能希望从中创建一个函数,只是return customConfirmFunction( );
为了使其更易于阅读和面向未来,以防您需要为其添加更多逻辑。
回答by Jelly
If you don't wanner go to another page, you can use ajax, or set your form action to the same request php file.
如果您不想转到另一个页面,您可以使用ajax,或者将您的表单操作设置为相同的请求php 文件。
回答by Abhishek Singh
You can do something like this :-
你可以做这样的事情:-
<script type="text/javascript">
var valueText=document.getElementById("username");
function confirmPopUp()
{
var bul=confirm("Are you sure to remove ID"+valueText.value);
if(bul==true)
{
//statements
}
else
{
//statements
}
}
</Script>