Submit function jquery not working - javascript

I have two trigger click function in my form one is a link and the other one is a submit button.
The link trigger ('.quiz-progress') is working well to submit form with id $("#quiz-question-answering-form") of form, but the button ('#edit-navigation-back') is not. It cannot submit $("#quiz-question-answering-form").submit();
It makes me confuse because when I change the submit function in the button to alert method is working.
so what i need is when user click radio box the button automatically submit it..?
Here's the full code
//fist trigger link link ( working well)
$(".query-once-1-processed").click(function(){
if ( $(this).hasClass("selected") ) {
//do something it does have the protected class!
$("#edit-navigation-skip").hide();
$('#edit-navigation-submit').show();
$(".quiz-progress").click(function(e) {
e.preventDefault(); // if desired...
$("#quiz-question-answering-form").submit();
});
} else{
$('#edit-navigation-submit').hide();
}
});
//second trigger (button) cannot submit the form (not working)
$(".multichoice-row").click(function(){
if ( $(this).hasClass("selected") ) {
$("#edit-navigation-back").click(function() {
//e.preventDefault(); // if desired...
$("#quiz-question-answering-form").submit();
//alert ("ok");
});
}
});
here is the html markup
<form class="answering-form" action="/learning/node/27/take/2" method="post" id="quiz-question-answering-form" accept-charset="UTF-8">
<table>
<tbody>
<tr class="multichoice-row query-once-processed-1"><td width="35"><div class="form-item form-type-radio form-item-question-1-answer-user-answer">
<input type="radio" id="edit-question-1-answer-user-answer-142" name="question1" value="142" class="form-radio" />
</div>
</td><td><p>Hulk</p>
</td> </tr>
</tbody>
</table>
<input type="submit" id="edit-navigation-back" name="op" value="< Soal Sebelumnya" class="form-submit" />
</form>

Related

After submit, input text disappears and reload the page(AJAX)

Why my page reloads even I used ajax to it and it also disappears my input text after clicking the submit button. I already used show to solve it but it doesn't work.
<form method="POST" action="<?php $_SERVER['PHP_SELF'];?>">
<input type="text" name="name" id="name">
<span id="namenotif" style="color:red;"> <span>
<br>
<input type="text" name="price" id="price">
<span id="pricenotif" style="color:red;"> <span>
<br>
<input type="submit" name="submit" id="save"><br>
</form>
<script>
$(document).ready(function() {
$(document).on("click","#save",function(){
var name = $("#name").val();
var price = $("#price").val();
if(name==""){
$("#namenotif").html("Enter a name");
$("#name").show("fast");
$("#save").show("fast");
}
else if(price==""){
$("#pricenotif").html("Enter a price");
$("#price").show("fast");
$("#save").show("fast");
}else{
$.ajax({
url:"addproduct.php",
type:"POST",
data:{name:name,price:price},
success:function(data){
alert("Successful");
}
});
}
});
});
</script>
add return false to end of function, that handle click event
Two solutions:
Change the button type='submit' to type='button'
or ( and preferably )
Change the event listener to listen for the form's onSumbit event then call event.preventDefault or in jQuery, I think you just do return false in the callback.
The form is being submitted I think, because it is the default behavior of submit button to submit the form, no matter if you used ajax or not. so you can prevent the default behavior by simple adding a code in jquery. Modify the code like this:
$(document).on("click","#save",function(e){
e.preventDefault();
...............
Rest of the codes can remain same. so only prevent the default action, it should work.
Its because of type="submit"
Use
<input type="button" name="submit" id="save"><br>
Or
<a href="javascript:void(0) id="save">
or
jquery's preventDefault();

How to validate an html5 form and show error tips on a button click?

I have a button submit inside a form and just a normal button outside of it. I want to validate a form:
function myButtonHandler(evt) {
if (myForm.checkValidity()) {
alert("yes");
} else {
alert("no");
}
}
This doesn't show the standard error tips inside of input elements when they're invalid when I click on a button -- ones shown by a browser when I click the submit button. How can I get these validation message to pop up when I click on my normal button when the form is invalid?
<form id="my_form">
<input type="text" placeholder="Name" required="true"/>
<input type="submit" id="submit" value="go" />
</form>
No jquery.
You'll need to add the code you've shown to a function that is set up as the click event callback for the normal button:
var myForm = document.querySelector("form"); // reference to form
var btn = document.querySelector("[type='button']"); // reference to normal button
// Set up click event handling function for normal button
btn.addEventListener("click", function(){
if (myForm.checkValidity()) {
alert("yes");
} else {
alert("no");
}
});
<form>
<input type="text" required>
<button type="submit">submit</button>
</form>
<button type="button">Check Validity</button>
If you just want to show the normal browser's validation errors, you can make the second button also a submit button. It's OK for the button to be outside of the form as long as you tie it back to the form with the form attribute.
<form id="theForm">
<input type="text" required>
<button type="submit">submit</button>
</form>
<button type="submit" form="theForm">Check Validity</button>

Jquery onclick submit and jquery function

I have a button, when click, it do a jquery function and submit form too.
This is my HTML code:
<?php echo Form::open(array("class"=>"form-horizontal","method"=>"POST" ,"id"=>"frmMainOrders","enctype" => "multipart/form-data" )); ?>
<div class="search">
<tr class="tblAdvancedSearch">
<th scope="row">備考</th>
<td>
<input class="input_text_search" type="text" name="multi_column" id="multi_column_search" value=""/>
</td>
</tr>
<input type="submit" id="btn_submit" value="検 索" name="adv_search">
</div>
<?php echo Form::close();?>
This is my script jquery:
$('.search').on('click', function() {
showAdvancedForm(); // when click in div class=search, it do a jquery function name showAdvanceForm().
});
function showAdvancedForm() {
if($(".tblAdvancedSearch").css('display') == 'none') {
$(".tblAdvancedSearch").css('display', 'table-row');
} else {
$(".tblAdvancedSearch").css('display', 'none');
}
}
I have tried:
<input type="submit" id="btn_submit" value="検 索" name="adv_search" onclick="$('form').submit()">
This way allow me submit form, but my controller can not get attribute name="adv_search", so my function doesn't work.
I have tried preventDefault() and $('#btn_submit').click(false).
But both of them prevent all submit and jquery function.
Is there a way to submit the form but prevent ONLY jquery function when I click submit button?
You can use e.target.name to find out the name of the element. So based on that you can conditionally fire the method showAdvancedForm() .
e.target - Get the element that triggered a specific event
$('.search').on('click', function(e) {
if (e.target.name == "multi_column") {
console.log('calling method : showAdvancedForm');
showAdvancedForm(); // when click in div class=search, it do a jquery function name showAdvanceForm().
}
});
function showAdvancedForm() {
if ($(".tblAdvancedSearch").css('display') == 'none') {
$(".tblAdvancedSearch").css('display', 'table-row');
} else {
$(".tblAdvancedSearch").css('display', 'none');
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="page.html" class="form-horizontal">
<div class="search">
<tr class="tblAdvancedSearch">
<th scope="row">備考</th>
<td>
<input class="input_text_search" type="text" name="multi_column" id="multi_column_search" value="" />
</td>
</tr>
<input type="submit" id="btn_submit" value="検 索" name="adv_search">
</div>
</form>
If you have a form and you want to send data via Ajax you should do something like this:
$('#formID').submit(function(e) {
e.preventDefault();
var submitButtonValue=$(this).children('input[type=submit]').val(); //This is 検 索
var data=$(this).serialize()+ "&adv_search="+submitButtonValue;
//probably your ajax call here...
})
Serialize is a function that you can get all your inputs' values in your form by it, and you also can easily send it via Ajax
You should use submit, because it also will check validations and more

trying to submit form with submit button as button

I am trying to submit as form , but it is not working , i want to get the submit button disabled once clicked.
<form action="Buy" method="POST" >
<input type="hidden" name="TID" value="${TID}"/>
<td align="center" valign="middle">
<button onclick="this.disabled = true; document.getElementById('up7913').disabled = false;" type="submit" name="down7913" id="down7913">Subscribe Now</button></td>
</form>
Bind to the submit event and disable the button.
document.getElementById('form').addEventListener('submit', function(event) {
event.preventDefault();
this.down7913.disabled = true;
// handle POST request
});
DEMO: http://jsfiddle.net/jefvw66q/1/

Why can't I submit my form?

I have a modal popup, that I want to hide when the 'shade' is clicked, but not when the contents are clicked. For this purpose I have:
$('#shade').click(function(){
$('#shade').fadeOut();
}).children().click(function(e){
return false;
});
The problem is I have a form inside that I want to submit, and this code prevents the form from submitting, so I have:
$('#shade-form-submit').click(function(){
document.getElementById('shade-form').submit();
});
However this results in the following error in the console:
[13:07:21.145] TypeError: document.getElementById(...).submit is not a function # ...
What's going on? For reference, my <form> tag:
<form action='.' method='post' id='shade-form'>
<div>
<input type='text' />
</div>
<div>
<div>
<input type='submit' />
</div>
</div>
</form>
You need to exclude form inputs from the click handler you have on children():
$('#shade')
.click(function(){
$('#shade').fadeOut();
})
.children().not(':input').click(function(e){
return false;
});

Categories

Resources