I am using a thick box and want to refresh parent page when thicbox is closing. I use below code but it works sometime and dont work in some situations... waht is the problem with it?
<script>
function close2()
{
parent.tb_remove();
parent.location.reload(1)
}
</script>
<input type="submit" name="save_new_task" id="save_new_task" onclick="close2()" style="width: 100px;text-align: center;"
class="button button-primary button-medium" value="<?Php _e("register","creat_mysite");?>"/>
Maybe it is because your submitting the form before the JavaScript get executed? If you want to submit your form and then perform a Javascript function you can try this:
$(document).ready(function()
{
$('#save_new_task').click(function(e)
{
e.preventDefault();
//Serialize your form
//POST request
//Maybe also try window.opener instead of parent
//window.opener refers to the window that called window.open( ... ) to open the window from which it's called
//window.parent refers to the parent of a window in a <(i)frame>
window.opener.tb_remove();
window.opener.location.reload();
});
});
i dont know the context but from what i could figure out try the below
Try this:
<script>
function close2()
{
parent.tb_remove();
parent.location.reload(1);
return true;
}
</script>
<form onsubmit="return close2()" >
<input type="submit" name="save_new_task" id="save_new_task" style="width: 100px;text-align: center;" class="button button-primary button-medium" value="<?Php _e("register","creat_mysite");?>"/>
</form>
Related
In my code, onclick function of addDvcPeople() works fine without form.
But once form added, the onclick function of addDvcPeople() works fail.
Here is the code without form which works fine:
<div class="dvcOnShlv" id="dvcOnShlv">
<!--<form action="modify_idc_addDVC.php?act=add&table=IDC" method="POST">-->
<table border="1px" cellspacing="0" id="staTable">
<tr>
<h2 align="center">IDCtable</h2>
</tr>
<tr>
<td style="width:15%" id="addDvcWorker">engineer<br /><input type="button" id="addDvcPeople" onclick="addDvcPeople()" value="addpeople"></td>
</tr>
</table>
<!--</form>-->
</div>
My addDvcPeople() code is:
<script>
function addDvcPeople()
{
alert("test");
}
</script>
Once "form" sentence is added, function addDvcPeople() did nothing.
I don't know why.Who can help me ?
The main problem that you have is that you have given your button the same ID as your function name. You need to ensure that these are different, or your function will always come through as undefined:
function addDvcPeople() {
alert("test");
}
<form>
<input type="button" id="addDvcPeople" onclick="addDvcPeople()" value="Button">
</form>
Here this is shown working with a different ID:
function addDvcPeople() {
alert("test");
}
<form>
<input type="button" id="addDvcPeopleID" onclick="addDvcPeople()" value="Button">
</form>
A secondary problem you'll encounter is that form submission has a default behaviour of actually submitting a form, even without a submit input, meaning that your form will disappear after the button is clicked:
function addDvcPeople() {
alert("test");
}
<form>
<button onclick="addDvcPeople()">Button</button>
</form>
To resolve this, you need to override the default form submission by passing through the event:
function addDvcPeople(event) {
event.preventDefault();
alert("test");
}
<form>
<button onclick="addDvcPeople(event)">Button</button>
</form>
Note that in the above example, the page does not refresh, and the alert appears.
Hope this helps! :)
You just do not set the duplicate id and function name
I am using jcryption for encrypting the form.It works fine if i use submit button in form. Instead if i use button and submit the form manually my jcryption method is not getting called.
below is my code
<html>
<head>
<script type="text/javascript">
$(document).ready(function() {
$("#login").bind('click', function(){
document.authenticatorform.username.value=$("#username").val();
document.authenticatorform.password.value=$("#password").val();
alert('outer hello');
$("#authenticatorform").jCryption({
getKeysURL:"<%=request.getContextPath()%>/keypairrequest",
beforeEncryption:function() {
alert('inner hello');
document.authenticatorform.submit()
return true; },
encryptionFinished:function(encryptedString, objectLength) {return true;}
});
});
});
</script>
<body>
<form:form method="post" action="login.htm" name="authenticatorform" id="authenticatorform">
<input type="hidden" name="username"/>
<input type="hidden" name="password"/>
</form:form>
<input type="button" id="login"/>
</body>
</html>
In the code only outer alert is printing.
Is it possible to call jcryption in other than submit button?
Any help will be greatly appreciated!!!!!
Try using on click function instead of bind
Try this:
$("#login").on('click', function(){
//your codes goes here
}
I have some javascipt code here that validates a user form. When the user inputs the correct answer it tells them and gives them the link to the next question. At least, that's what it is supposed to do. When i click the form it reloads the page but it should not because i added return false.
the div tra holds 35
and the div usermsg is the user inputted value.
<script>
$("#submit").click(function(){
var clientmsg6 = $("#usermsg").val();
var rightanswer = $("#tra").val();
if (clientmsg6<>rightanswer)
{
$("#confirm").html("<h2>Sorry, wrong answer.</h2>");
}
else
{
$("#confirm").html("<a href='#' onclick='play();' style='font-size:20px;' id='new1'>Click here for Question 2</a>");
}
return false;
});
</script>
Any ideas why this is not working?
It should be
if (clientmsg6 != rightanswer)
not
if (clientmsg6<>rightanswer)
To prevent a form submission, you need to return false on the form itself instead of on the submit button. Your code should become:
HTML
<form action="page.php" method="post">
<input id="usermsg" type="text" name="answer" />
<input id="submit" type="submit" value="Submit" />
</form>
JS (please note the line where you have clientmsg6, you have a syntax error)
$("#myform").on('submit', function(){
var clientmsg6 = $("#usermsg").val();
var rightanswer = $("#tra").val();
if (clientmsg6 != rightanswer) { //This line was also wrong, should be != instead of <>
$("#confirm").html("<h2>Sorry, wrong answer.</h2>");
}
else {
$("#confirm").html("<a href='#' onclick='play();' style='font-size:20px;' id='new1'>Click here for Question 2</a>");
}
return false;
});
Alternatively, you can keep your existing code by changing your submit button to be just a plain old button, but you will lose the extra functionality of the user being able to hit the enter key and performing the same action.
<form action="page.php" method="post">
<input id="usermsg" type="text" name="answer" />
<input id="submit" type="button" value="Submit" />
</form>
Instead of using .html(), try using .text()
if #submit is a link tag otherwise use the form ID and the submit event
$("#submit").click(function(e){
e.preventDefault()
...
...
...
});
You need to attach handlers once the document has finished loading.
Wrap your script in the following
<script>
$(function() {
// script
});
</script>
I am trying to open a new popup window, insert values into database, after that return one value to current window. After I open a new popup window and click return, it returns the value but when I click on submit and return it after that, the value doesn't get returned. I think that's because the new window gets refreshed by the submit button. That's why it doesn't return the value.
Main Page
<form></form>
<form>
<input name="maparea" size="2" type="TEXT">
<input onclick='targetitem = document.forms[1].maparea; dataitem = window.open("popup.php", "dataitem", "toolbar=no,menubar=no,scrollbars=yes"); dataitem.targetitem = targetitem' value="Get Value" type="button">
</form>
Popoup window
<script>
function select_item(item){
targetitem.value = item;
top.close();
return false;
}
</script>
<form action="" method="post">
<input type="submit" name="sub" value="Submit" />
<input type="button" name="re" value="Return" onclick='return select_item("3")' />
</form>
Any solution for that?
I want to submit what I want first after that return the value
In the popup, hook an onclick event on your submit button so it executes before the submit.
Then in the onclick handler do:
window.opener['dataitem'] = <your return value>;
Then after the submit, your parent window will have that value, and you can access it like this:
var somevariable = window['dataitem'];
function setColor(color){ if (opener && !opener.closed){ opener.document.theForm.theField.value = color; opener.focus(); } window.close(); } ... <td width="30" bgcolor="#FFFF00" onclick="setColor(this.bgColor)"> </td>
Read more at http://www.codingforums.com/showthread.php?t=61319#gkH9pd6gdgvxYqQZ.99
How about this?
Open your popup.
In the popup: submit the form via AJAX (to avoid a page refresh).
In the popup: In the success handler for your AJAX call, grab the desired value, pass it back using window.opener, then close the popup.
I am using a simple Javascript toggle function with the following code.
<script>
function add_more(){
if (document.form.more[0].checked==true)
{
document.getElementById('moreTxt').style.display="block";
}
else if (document.form.more[1].checked==true)
{
document.getElementById('moreTxt').style.display="none";
}
}
</script>
do want to enter something more ?
<form name="form">
<input type="radio" value="yes" name="more" onclick="add_more()" /> Yes
<input type="radio" value="No" name="more" onclick="add_more()" /> No
<div id="moreTxt" style="display:none">
hi you can enter more here
<textarea rows="3" cols="4">
</textarea>
</div>
</form>
The Problem is if I click on 'yes', and for some reason I refresh the page, then the 'yes' radio button remains checked but moreTxt div hides (i.e. its default visiblity mode).
How should I tackle this problem?
Check the value of the control on document ready and adjust the div visibility accordingly.
This will reset all your input on load:
<script>
function resetChkBox() {
var e=document.getElementsByTagName('input');
var i=0;
while(i<e.length) {
if(e[i++].type=='radio') {
e[i].checked=false;
}
}
}
</script>
<body onload="resetChkBox()">
you should perform you add_more function onDucomentReady
Assuming you're using jquery:
$(document).ready(function() {
add_more();
}
)
or use plain javascript equivalent of document.ready()
If this is to be performed without jquery which should be the case since including jquery for such small thing will be overkill :
document.attachEvent("onDOMContentLoaded", function(){
ready()
});
In ready function check the value of the radio button.