I have form that has 2 radio buttons(Yes and No) and a text box. If the user clicks Yes it enables the text box you can input information and it is uploaded to the database including the value from the radio buttons. If you click no it disables the text box and suppose to upload the value of the radio box only to the database. But I am not getting that.
<input type="radio" name="TermLease" value="No" onclick="TermLeaseMonths.disabled=true">No
<input type="radio" name="TermLease" value="Yes" onclick="TermLeaseMonths.disabled=false">Yes |
How many months:<input type="hidden" name="TermLeaseMonths" value="0" />
<input type="text" name="TermLeaseMonths" id="TermLeaseMonths" size="1" disabled="true">
I have a hidden input type that uploads the value. But when I click yes it does not disable the text box. Not sure where I am going wrong.
You forgot give id for the text field. Try this
<input type="radio" name="TermLease" value="No" onclick="TermLeaseMonths.disabled=true">No
<input type="radio" name="TermLease" value="Yes" onclick="TermLeaseMonths.disabled=false">Yes |
How many months:<input type="hidden" name="TermLeaseMonths" value="0" />
<input type="text" name="TermLeaseMonths" id="TermLeaseMonths" size="1" disabled="true">
Try this on click of radio button we can do by this way
$('#radio').click(function () {
if (("#radio").val() == "") {
$('class_name_oftextBox').attr("disabled", true);
} else {
$('class_name_oftextBox').removeAttr("disabled");
}
});
Or you can do this as
$('#enable').click(function () {
$('#textBox').removeAttr("disabled")
});
$('#disable').click(function () {
$('#textBox').attr("disabled", "disabled");
});
Demo jsFiddle
HTML
<form>
<span style="float: left;">
<label><input type="radio" name="TermLease" value="No" onclick="ShowHideTextbox('leaseMonths', false)" />No</label>
<label><input type="radio" name="TermLease" value="Yes" onclick="ShowHideTextbox('leaseMonths', true)"/>Yes</label>
</span>
<span id="leaseMonths" style="display: none; float: left;">
<span> | </span> <span>How many months: </span>
<input type="text" name="TermLeaseMonths" id="TermLeaseMonths" size="1" />
</span>
</form>
JS
var previousNumberOfMonths = "0";
function ShowHideTextbox(elementId, show) {
var link = document.getElementById(elementId);
var textbox = document.getElementById("TermLeaseMonths");
if (show){
textbox.value = previousNumberOfMonths;
link.style.display = 'block';
}
else {
link.style.display = 'none';
previousNumberOfMonths = textbox.value;
document.getElementById("TermLeaseMonths").value = "0";
}
}
Related
I have some jQuery checking to see when a human clicks a check button but if the page loads with the button marked as checked already, the jQuery doesn't work.
$ (function(){
var requiredCheckboxes = $('.options :checkbox[required]');
requiredCheckboxes.change(function(){
if(requiredCheckboxes.is(':checked')) {
requiredCheckboxes.removeAttr('required');
} else {
requiredCheckboxes.attr('required', 'required');
}
});
});
What I need this to do is if the checkbox is already checked, it removes the required attribute. Why is it not working when a variable is posted to the page and PHP adds checked="checked" to the input?
Thanks!
You have to use this as selector on checking if the clicked checkbox is checked or not. this refers to the clicked checkbox. You can use prop() to add and remove required
var requiredCheckboxes = $('.options');
requiredCheckboxes.change(function() {
if ($(this).is(':checked')) {
$(this).prop('required', false);
} else {
$(this).prop('required', true);
}
});
input:required {
box-shadow: 4px 4px 20px rgba(200, 0, 0, 0.85);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class='options' type="checkbox" required>
<input class='options' type="checkbox" required>
<input class='options' type="checkbox" required>
<input class='options' type="checkbox" required>
Update: You can use a class for the required checkbox and use prop() to add and remove required property.
var requiredCheckboxes = $('.required-options')
//On page Loads
if (requiredCheckboxes.is(':checked')) {
requiredCheckboxes.prop('required', false);
} else {
requiredCheckboxes.prop('required', true);
}
//Event listener
requiredCheckboxes.change(function() {
if (requiredCheckboxes.is(':checked')) {
requiredCheckboxes.prop('required', false);
} else {
requiredCheckboxes.prop('required', true);
}
});
input:required {
box-shadow: 4px 4px 20px rgba(200, 0, 0, 0.85);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class='required-options' type="checkbox" required checked>
<input class='required-options' type="checkbox" required>
<input class='required-options' type="checkbox" required>
<input class='required-options' type="checkbox" required>
Html:
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
js:
$("document").ready(function(){
$('body').find('input:checkbox').prop('checked', "checked");
});
jsfiddle
I have a simple survey form i designed but for some reason one line of code isnt working.
In question 5. If the user selected the radio button YES then they must enter something in the textarea or the form must not be sent.
If no then the textarea should be locked so nothing can be entered.
Here is the code: Thanks for the help ladies and gentleman
5. Are you using other non-franchise service centres?
<br>
*if yes is there any other reason you would do so other than price
<br>
<input type="radio" name="question5"
<?php if (isset($question5) && $question5=="Yes") echo "checked";?>
value="Yes">Yes
<br>
<input type="radio" name="question5"
<?php if (isset($question5) && $question5=="No") echo "checked";?>
value="No">No
<br>
<textarea name="question5" rows="5" cols="40"><?php echo $question5 </textarea>
Option1: hide the textarea
function check5(selectedType){
if (selectedType == 'Yes'){
document.getElementById('5textarea').style.display = 'block';
} else{
document.getElementById('5textarea').style.display = 'none';
}
}
5. Are you using other non-franchise service centres?
<br>
*if yes is there any other reason you would do so other than price
<br>
<input type="radio" name="question5" value="Yes" onclick="check5(this.value)"> Yes
<br>
<input type="radio" name="question5" value="No" onclick="check5(this.value)"> No
<br>
<textarea id="5textarea" name="question5" rows="5" cols="40" style="display:none"></textarea>
Option2: enable/disable the textarea (NOT TESTED):
5. Are you using other non-franchise service centres?
<br>
*if yes is there any other reason you would do so other than price
<br>
<input type="radio" name="question5" value="Yes" onclick="check5(this.value)"> Yes
<br>
<input type="radio" name="question5" value="No" onclick="check5(this.value)"> No
<br>
<textarea id="5textarea" name="question5" rows="5" cols="40" disabled="true"></textarea>
and change the javascript to:
function check5(selectedType){
if (selectedType == 'Yes'){
document.getElementById("5textarea").disabled = false;
} else{
document.getElementById("5textarea").disabled = true;
}
}
You have to do it with javascript since PHP only runs on the server, not in the browser!
In this code, the textarea is disabled if not needed, and if "Yes" is checked and the textarea doesn't contain enough text, an error appears when submitting the form:
// Javascript:
function validate() {
var radioYes = document.getElementById('radioYes');
var textarea = document.getElementById('textarea');
if (radioYes.checked && textarea.value.length < 10) {
alert('Enter at least 10 characters!');
textarea.focus();
return false;
}
}
function toggle(value) {
document.getElementById('textarea').disabled = value;
}
/* CSS: */
* {
font-family: sans-serif;
margin: 4px;
}
<!-- HTML/PHP: -->
<form action="#">
5. Are you using other non-franchise service centres?<br>
*if yes is there any other reason you would do so other than price?<br>
<input type="radio" name="question5" value="Yes" onchange="toggle(this.selected)" id="radioYes">Yes<br>
<input type="radio" name="question5" value="No" onchange="toggle(!this.selected)">No<br>
<textarea id="textarea" name="question5" rows="5" cols="40" disabled></textarea><br>
<input type="submit" value="Send" onclick="return validate()" />
</form>
JSFiddle
I want to get radio button values and send them through AJAX to PHP.
My AJAX is running but is currently inserting a 0 in each row, so it's not picking up the value from the radio button. Any help would be appreciated.
$("#save_privacy").submit(function() {
var message_pri = $("#message_pri").val();
var follow_pri = $("#follow_pri").val();
var post_pri = $("#post_pri").val();
var check7 = $("#check7").val();
var s = {
"message_pri": message_pri,
"follow_pri": follow_pri,
"post_pri": post_pri,
"check": check7
}
$.ajax({
url: 'edit_check.php',
type: 'POST',
data: s,
beforeSend: function() {
$(".privacy_info").html("<img src=\"style/img/ajax/load2.gif\" alt=\"Loading ....\" />");
},
success: function(data) {
$(".privacy_info").html(data);
}
});
return false;
});
<form id="save_privacy">
<table align="center" width="70%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td style="padding: 5px;">
<b>Message Buttun: </b>
</td>
<td style="padding: 5px;">
<input type="radio" id="message_pri" name="message_pri" value="1" /> ON
<input type="radio" id="message_pri" name="message_pri" value="2" /> OFF
<input type="radio" id="message_pri" name="message_pri" value="3" /> FOLLOWERS
</td>
</tr>
<tr>
<td style="padding: 5px;">
<b>Follow Buttun: </b>
</td>
<td style="padding: 5px;">
<input type="radio" id="follow_pri" name="follow_pri" value="1" /> ON
<input type="radio" id="follow_pri" name="follow_pri" value="2" /> OFF
</td>
</tr>
<tr>
<td style="padding: 5px;">
<b>Who Can Post On Your Profile: </b>
</td>
<td style="padding: 5px;">
<input type="radio" id="post_pri" name="post_pri" value="1" /> Evry one
<input type="radio" id="post_pri" name="post_pri" value="2" /> Followers
</td>
</tr>
<tr>
<td colspan="2" style="padding: 5px;">
<input type="hidden" id="check7" value="save_privacy" name="check7" />
<input class="small color blue button" type="submit" value="Save" />
</td>
</tr>
</table>
<div class="privacy_info"></div>
</form>
Firstly you have a lot of duplicated id attributes, which is incorrect. Use classes instead, then use the :checked selector to get the specific instance of the radio which was selected.
Try this:
<input type="radio" class="message_pri" name="message_pri" value="1" /> ON
<input type="radio" class="message_pri" name="message_pri" value="2" /> OFF
<input type="radio" class="message_pri" name="message_pri" value="3" /> FOLLOWERS
var message_pri = $(".message_pri:checked").val();
And so on for your other radio inputs.
dont use id two time first thing
now for selected value of radio box use
$("input:radio[name=post_pri] :selected").val();
I would like to add that it is best to use the onChange event instead of the onClick event on the radio fieldset button call to the AJAX function. I am not sure why, but in this case it posts the correct value every time. When using the onClick event it sometimes posts a value different from the checked value. It is not much but it will definitely save somebody somewhere from a slight headache.
Example of the radion button group:
<fieldset **onChange="return rating_score()"** id="rating_selectors" data-
role="controlgroup" data-type="horizontal">
<input <?php if (!(strcmp($row_rs_new_rating['rating_value'],"1"))) {echo
"checked=\"checked\"";} ?> type="radio" name="rating" id="ratings_0"
value="1" />
<label title="1" for="ratings_0"></label>
<input <?php if (!(strcmp($row_rs_new_rating['rating_value'],"2"))) {echo
"checked=\"checked\"";} ?> type="radio" name="rating" id="ratings_1"
value="2" />
<label title="2" for="ratings_1"></label>
<input <?php if (!(strcmp($row_rs_new_rating['rating_value'],"3"))) {echo
"checked=\"checked\"";} ?> type="radio" name="rating" id="ratings_2"
value="3" />
<label title="3" for="ratings_2"></label>
<input <?php if (!(strcmp($row_rs_new_rating['rating_value'],"4"))) {echo
"checked=\"checked\"";} ?> type="radio" name="rating" id="ratings_3"
value="4" />
<label title="4" for="ratings_3"></label>
<input <?php if (!(strcmp($row_rs_new_rating['rating_value'],"5"))) {echo
"checked=\"checked\"";} ?> type="radio" name="rating" id="ratings_4"
value="5" />
<label title="5" for="ratings_4"></label>
</fieldset>
Example of the AJAX function:
<script type="text/javascript">
function rating_score ( txt_rating )
{ $.ajax( { type : "POST",
data: {"txt_captcha" : $("#txt_captcha").val(), "txt_rating" :
$("input[name=rating]:checked").val()},
url : "functions/reviewrater.php",
success : function (txt_rating)
{
$('#rating-container').load(document.URL + ' #rating-container');
$('span.stars').stars();
},
error : function ( xhr )
{ alert( "error" );
}
} );
return false;
}
</script>
Quick explanation:
The onChange event in the fieldset sends the value of the checked radio button to the AJAX function. I have added validation for the rest of the elements on the page, so the <?php if (!(strcmp($row_rs_new_rating['rating_value'],"3"))) {echo "checked=\"checked\"";} ?> compares the value stored in the rating session and retrieves the value again, so the user does not have to click on the rating again once they are warned that some of the other elements are empty. It looks much more complicated than it is, but all I actually wanted to say was to use the onChange instead of the onCLick event. :-)
You can visit this page to see the above code in action:
Rental Property Reviews Page
Try this use serialize function check serialize here
$("#save_privacy").submit(function(){
var serialise = $("#save_privacy").serialize();
$.ajax({
url:'edit_check.php',
type:'POST',
data:serialise,
beforeSend: function (){
$(".privacy_info") .html("<img src=\"style/img/ajax/load2.gif\" alt=\"Loading ....\" />");
},
success:function(data){
$(".privacy_info") .html(data);
}
});
return false;
});
For a project I want to implement a nice filtering mechanism with multiple checkboxes. I get the checkboxes to work correctly as well as a jQuery function to automatically POST the form when checking a checkbox.
Now I want to add a "select all" checkbox above the checkboxes, but I cannot seem to find the correct way. I have tried a dozen solutions for (somewhat) similar questions but I cannot get it to work correctly and consistent.
The HTML part is something like this:
<form action="<?php $_SERVER['PHP_SELF']?>" method="post">
<input type="checkbox" /> Select All colors<br/>
<label> <input type="checkbox" name="color[]" value="yellow"> Yellow</label><br/>
<label> <input type="checkbox" name="color[]" value="blue"> Blue</label><br/>
<label> <input type="checkbox" name="color[]" value="red"> Red</label><br/>
<label> <input type="checkbox" name="color[]" value="green"> Green</label><br/><br/>
<input type="checkbox" /> Select All brands<br/>
<label> <input type="checkbox" name="brand[]" value="Nike"> Nike</label><br/>
<label> <input type="checkbox" name="brand[]" value="Adidas"> Adidas</label><br/>
<label> <input type="checkbox" name="brand[]" value="SomeBrand"> SomeBrand</label><br/>
<label> <input type="checkbox" name="brand[]" value="SomeOtherBrand"> SomeOtherBrand</label><br/>
</form>
The jQuery part I use to post the form on each click on the checkbox (is not sufficient):
$('input:checkbox:').live('click',function() {
$(this).closest('form').submit();
});
My question now is what do I need for the jQuery part to make sure this works correctly?
I want to be able to click the label to deselect all from that group and select only that one checkbox. It also needs to POST the form for the array values. And lastly if all checkboxes are checked manually the "select all" one has to be checked as well.
Hopefully someone can help me out as I am stuck with this for a long time...
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$("#colorall").click(function()
{
var checked_status = this.checked;
$(".color").each(function()
{
this.checked = checked_status;
});
});
$(".color").click(function(){
if($(".color").length == $(".color:checked").length) {
document.getElementById("colorall").checked = true;
} else {
$("#colorall").removeAttr("checked");
}
});
$("#brandall").click(function()
{
var checked_status = this.checked;
$(".brand").each(function()
{
this.checked = checked_status;
});
});
$(".brand").click(function(){
if($(".brand").length == $(".brand:checked").length) {
document.getElementById("brandall").checked = true;
} else {
$("#brandall").removeAttr("checked");
}
});
});
</script>
<form action="<?php $_SERVER['PHP_SELF']?>" method="post">
<input type="checkbox" id="colorall" /> Select All colors<br/>
<label> <input type="checkbox" name="color[]" value="yellow" class="color"> Yellow</label><br/>
<label> <input type="checkbox" name="color[]" value="blue" class="color"> Blue</label><br/>
<label> <input type="checkbox" name="color[]" value="red" class="color"> Red</label><br/>
<label> <input type="checkbox" name="color[]" value="green" class="color"> Green</label><br/><br/>
<input type="checkbox" id="brandall"/> Select All brands<br/>
<label> <input type="checkbox" name="brand[]" value="Nike" class="brand"> Nike</label><br/>
<label> <input type="checkbox" name="brand[]" value="Adidas" class="brand"> Adidas</label><br/>
<label> <input type="checkbox" name="brand[]" value="SomeBrand" class="brand"> SomeBrand</label><br/>
<label> <input type="checkbox" name="brand[]" value="SomeOtherBrand" class="brand"> SomeOtherBrand</label><br/>
</form>
This is the checkbox
<input id="c1" type="checkbox" name="hour[]" class="create-daily" value="1" /><br />
<input id="c2" type="checkbox" name="hour[]" class="create-daily" value="2" /><br />
<input id="c3" type="checkbox" name="hour[]" class="create-daily" value="3" /><br />
I have a dialog
<div id="form_input" title="Konfirmasi">
<?php $data['hour'] = $this->input->post('hour');?>
<table>
<?php echo form_open('booking/ok'); ?>
<input type="hidden" value='' id="id" name="id">
<input type="hidden" value="<?php echo $sdate?>" id="sdate" name="sdate" />
<?php echo "Anda memesan room : <br/>Tanggal :<b> ".$sdate."</b><br>";?>
Jam : <b><span class="jambooking"></span></b>
<tr>
<td>
<div class="element">
<label class="Norm">Jam:</label>
<input type="text" name="jam" class="jambooking" id="jam" minlength="2" value="" />
</div>
<label class="Norm">User:</label>
<input type="text" name="user" class="required text" minlength="2" />
</div>
I have javascript
$(".create-daily").live("click",function(){
if ($(this).attr("checked")){
//var date = $(this).attr("date");
//$('#date').val(date);
$( "#form_input" ).dialog( "open" );
$(".jambooking").html( $(":checked").val());
}
});
I use a span and input with a class set to jambooking. When I checked a check box and show a dialog, the span shows a value.
When I set to the input class jambooking the value does not show.
How would I load a value to the text field?
Try:
$(".create-daily").live("click",function(){
if ($(this).is(":checked")){
var checkedVal = $(this).val();
//$('#date').val(date);
$( "#form_input" ).dialog( "open" );
$("span.jambooking").html(checkedVal); //for span
$("input.jambooking").val(checkedVal); //for textbox
}
});
Since you are using an input, you should use .val() instead of .html()
$(".jambooking").val( $(":checked").val());
$().html is for div. $().text is for <p>. You have to use different one for input field. Use $().val();
Change this one.
From
$(".jambooking").html( $(":checked").val());
To
$("input.jambooking:text").val( $(":checked").val());
check this out
$(".jambooking").val( $(":checked").val());
or check this link Jquery Text Editor
Hope it works.