admin 管理员组文章数量: 1086019
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: ')";/>
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: ')";/>
Share
Improve this question
edited May 14, 2012 at 4:43
jeffjenx
17.5k6 gold badges62 silver badges103 bronze badges
asked May 14, 2012 at 3:47
user127886user127886
3173 gold badges7 silver badges13 bronze badges
1
- 1 nothing to do with php, tag removed. – user557846 Commented May 14, 2012 at 3:53
5 Answers
Reset to default 2Replace the onclick
attribute by the following:
onclick="return confirm('Are you sure to remove ID ' +
document.getElementById('username').value + '?')"
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="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:
window.addEventListener('load', formHook, false);
And now, when the document has finished loading, the formHook
function will be called.
If you don't wanner go to another page, you can use ajax, or set your form action to the same request php file.
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>
You could 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.
本文标签: HTML javascript get value from inputStack Overflow
版权声明:本文标题:HTML javascript get value from input - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744089214a2531717.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论