How to convert pounds to kilograms using jQuery? - php

I have a form that allows the user to enter a weight, in either pounds or kilograms.
I'd like to store all of the data in kilograms, and using jQuery, would like the form to convert the weight_value to kilograms and change the weight_units option to "kg".
1 pound = 0.45359237 kilograms
The function should round the weight_value to the nearest kilogram (no decimals).
Any help you could provide would be appreciated!
<label> Weight:</label>
<input type="text" id="weight_value" />
<select id="weight_unit" />
<option value="lbs">lbs</option>
<option value="kg">kg</option>
</select>

Try this
function KtoLbs(pK) {
var nearExact = pK/0.45359237;
var lbs = Math.floor(nearExact);
return {
pounds: lbs,
};
}
var imperial = KtoLbs(10);
alert("10 kg = " + imperial.pounds + " lbs " );

My solution is here: http://jsfiddle.net/mVNDk/
$('#weight_unit').change(function(){
var v = $('input').val();
if($('#weight_unit').val() == 'lbs') {
$('input').val(v * 0.4535923);
} else {
$('input').val(v / 0.4535923);
}
});

try this
$(document).ready( function() {
$("#weight_value").keyup(function(){
var weight = parseFloat($("#weight_value").val());
if($("#weight_unit").val()=='lbs'){
//lbs value;
weight = (weight/0.45359237);
}else{
}
$("#weight_value_kg").val(Math.round(weight*100)/100);
});
});
<label> Weight:</label>
<input type="text" id="weight_value" />
<select id="weight_unit" />
<option value="lbs">lbs</option>
<option value="kg">kg</option>
</select>
<label> value:in kg</label>
<input type="text" id="weight_value_kg" />

Here is a try:
var kg = 0.45359237
$(document).ready(function() {
$("#weight_unit").change(function() {
if ($(this).val() == "kg") {
$("#weight_value").val(Math.round( $("#weight_value").val()*kg))
} else {
$("#weight_value").val(Math.round( $("#weight_value").val()/kg))
}
});
});
See it live on: http://jsfiddle.net/zeByX/
[EDIT] With the Math.floor(), you can get some weird results by switching from kg to pounds (try with 12 pounds and switch).

here's some code
$("#weight_unit").change(function() {
var result = 0;
if($(this).val() == "kg") {
result = Math.round($("#weight_value").val() * 0.45359237);
alert( result + " kg");
}
else {
result = Math.round($("#weight_value").val() / 0.45359237);
alert( result + " lbs");
}
});
you should replace the alert() functions for something you want to do with it

Checkout this fiddle buddy.
<label for='weight'>Weight</label>
<input type='text' id='weight' />
<select id='units'>
<option value='p'>Pound</option>
<option value='k'>Killogram</optoin>
</select>
<input type='text' id='result' />
with this script:
var weight = $('#weight'),
units = $('#units'),
result = $('#result');
// 1 pound = 0.45359237 kilograms
$(function() {
units.change(function() {
if ($(this).val() == 'p') {
// Weight is in Pound
result.val(weight.val() * 0.45359237);
}
else {
// Wiehgt is in Killogram
result.val(weight.val() / 0.45359237);
}
});
});

Related

Multiple else if in jQuery

I am doing dependent select box using jquery, ajax and php from same table of the database. Testpage
$(document).ready(function(){
$('.action').change(function(){
if($(this).val() != '')
{
var action = $(this).attr("id");
var query = $(this).val();
var result = '';
if(action == "name") {
result = 'category';
alert ('test1');
} else if (action == "category") {
result = 'material';
alert ('test2');
} else if (action == "material") {
result = 'source_light';
alert ('test3');
} else if (action == "source_light") {
result = 'color_temperature';
alert ('test4');
} else {
// result = 'color_temperature';
}
$.ajax({
url:"fetch_filter2.php",
method:"POST",
data:{action:action, query:query},
success:function(data){
$('#'+result).html(data);
}
})
}
});
});
I don't know why but it only works: test1 and test2. Test3 and 4 are not sent to php.
HTML file
Maybe something is wrong with select box? But these are just a select box with id's.
<select name="name" id="name" class="form-control action">
<option value="">Select name</option>
<?php echo $name; ?>
</select>
<br />
<select name="category" id="category" class="form-control action">
<option value="">Select category</option>
</select>
<br />
<select name="material" id="material" class="form-control">
<option value="">Select material</option>
</select>
<br />
<select name="source_light" id="source_light" class="form-control">
<option value="">Select source_light</option>
</select>
<br />
<select name="color_temperature" id="color_temperature" class="form-control">
<option value="">Select color_temperature</option>
</select>
What about something like this
$(document).ready(function(){
$('.action').change(function()
{
if($(this).val() != '')
{
var action = $(this).attr("id");
var query = $(this).val();
var result = '';
switch(action) {
case "name":
result = 'category';
alert ('test1');
break;
case "category":
result = 'material';
alert ('test2');
break;
case "material":
result = 'source_light';
alert ('test3');
break;
case "source_light":
result = 'color_temperature';
alert ('test4');
break;
default:
// result = 'color_temperature';
}
$.ajax({
url:"fetch_filter2.php",
method:"POST",
data:{action:action, query:query},
success:function(data){
$('#'+result).html(data);
}
})
}
});
});
Verify your drop down list under "action" class. As per your condition it won't trigger if your selected drop down item doesn't have value if($(this).val() != '').
Also make sure your expected drop down items have the value material for your test3 and source_light for your test4
Another suggestion: To load and trigger dynamic change event for drop down you can use below jQuery API:
$(document).on("change", ".action", function(){
//Your code here
});
If this doesn't solve your problem then please share your HTML or drop down data.
Updated answer: I can see there is no class "action" in your select/drop down items for material, source_light and color_temperature. That is why change actions of these drop down items are not catching in your $('.action').change(function() ... code block.

Change select dropdown based on first select dropdown?

I am trying to change the second select dropdown based on whether the first selected option contains an (m) or (ft).
My form HTML search is as follows:
<form id="search" action="/property_search" method="get" class="clearfix">
<select name="sqsize" id="sqsize" class="sqsize">
<option value="">sq.ft/sq.m:</option>
<option value="233.52m">233.52m</option>
<option value="233.52m">233.52m</option>
<option value="467.04m">467.04m</option>
<option value="2,513ft">2,513ft</option>
<option value="2,513ft">2,513ft</option>
<option value="5,026ft">5,026ft</option>
</select>
<select name="sqsizemin" id="sqsizemin" class="sqsizemin">
<option value="">Size Min:</option>
</select>
<input type="submit" class="submit" value="Search">
</form>
My Jquery is as follows:
<script type="text/javascript">
$(function(){
$('#sqsize').change(function () {
var options = '';
if($(this).val() == 'm') {
options = '<option value=""></option>';
}
else if ($(this).val() == 'ft') {
options = '<option value="6">6</option>';
}
$('#sqsizemin').html(options);
});
$('#sqsize').trigger('change');
});
Try something like this(using indexOf):
$(function(){
$("#sqsize").change(function(){
var options = '';
if($(this).val().indexOf('m') > -1) {
options = '<option value="">mm</option>';
}
else if ($(this).val().indexOf('ft') > -1) {
options = '<option value="6">6</option>';
}
$('#sqsizemin').html(options);
});
});
Working fiddle: https://jsfiddle.net/robertrozas/b0w61quf/
Try like this:
https://jsfiddle.net/kc5qqtco/
$(function(){
$('#sqsize').change(function () {
if($(this).val().match(/m/g)) {
$('<option value=""></option>').appendTo('#sqsizemin');
}
else if ($(this).val().match(/ft/g)) {
$('<option value="6">6</option>').appendTo('#sqsizemin');
}
});
});
Using indexOf will get you a working solution.
See the jsfiddle

Check sum values in a select

I need a validation before submitting this form:
<form action="<?php echo $editFormAction; ?>" method="POST" id="form1" name="contract">
<table class="myp-table">
<tr>
<td>Value Test</td>
</tr>
<?php do { ?>
<tr>
<td><select name="playerContract[]">
<option value="0" <?php if (!(strcmp(0, $row_datacontract['playerContract']))) {echo "selected=\"selected\"";} ?>>0</option>
<option value="1" <?php if (!(strcmp(1, $row_datacontract['playerContract']))) {echo "selected=\"selected\"";} ?>>1</option>
<option value="2" <?php if (!(strcmp(2, $row_datacontract['playerContract']))) {echo "selected=\"selected\"";} ?>>2</option>
<option value="3" <?php if (!(strcmp(3, $row_datacontract['playerContract']))) {echo "selected=\"selected\"";} ?>>3</option>
</select></td>
</tr>
<?php } while ($row_datacontract = mysql_fetch_assoc($datacontract)); ?>
<tr>
<td><input class="linkbuttonmp" name="contract" type="submit" value="Invio" /></td></tr>
</table>
</form>
The sum of the values selected by users mustn't exceed the default value which in my case is a value retrieved from my database.
I tried with jquery in this way:
var max = 3;
$("#form1 select").change(function () {
var selects = <?php echo $row_datacoach['coachContract']; ?>;
$("#form1 select").each(function () {
selects = selects + parseInt($(this).val());
});
if(selects >= max) {
$("#submit").attr("disabled","disabled");
} else {
$("#submit").removeAttr("disabled");
}
});
Unfortunately it doesn't work.
Can you help me?
UPDATE
Sorry, I made a mistake: the following is the script I actually tried:
<script type="text/javascript">
var max = <?php echo $row_datacoach['coachContract']; ?>;
$("#form1 select").change(function () {
var selects = 0;
$("#form1 select").each(function () {
selects = selects + parseInt($(this).val());
});
if(selects >= max) {
$("#submit").attr("disabled","disabled");
} else {
$("#submit").removeAttr("disabled");
}
});
</script>
The value fetched from database need to be assigned to variable max and the selects has to be initialized with value 0.
I think it should be
var max = <?php echo $row_datacoach['coachContract']; ?>;
$("#form1 select").change(function () {
var selects = 0;
$("#form1 select").each(function () {
selects = selects + parseInt($(this).val());
});
if(selects >= max) {
$("#submit").attr("disabled","disabled");
} else {
$("#submit").removeAttr("disabled");
}
});
Demo: Fiddle
add ID to form :
<form action="" method="POST" name="contract" id="form1">
then do :
var max = 3;
$("#form1 select").on('change', function () {
var selects = 0;
$("#form1 select").each(function () {
selects += parseInt(this.value, 10);
});
$("#submit").prop('disabled', selects >= max);
});

Multiply Checkbox Value with Input Box Value - Jquery

I am currently struggling to get the value I return from a checkbox to multiply with the value in the input box with jQuery.
I display a div when a checkbox is ticked to ask it for the quantity, thereafter I must multiply the quantity with the value of the checkbox.
Here is the current code:
$(document).ready(function () {
var sum = 0;
var qty = 0;
$("input[type=checkbox]").change(function () {
recalculateQty();
recalculateCheck();
if ($('#1').is(':checked')) {
$('#checked-1').show();
} else {
$('#checked-1').hide();
}
if ($('#2').is(':checked')) {
$('#checked-2').show();
} else {
$('#checked-2').hide();
}
if ($('#3').is(':checked')) {
$('#checked-3').show();
} else {
$('#checked-3').hide();
}
});
function recalculateQty() {
$('.qty').keyup(function () {
$('.qty').each(function () {
qty += parseInt(this.value, 10);
recalculateCheck();
});
});
}
function recalculateCheck() {
var sum = 0;
$("input[type=checkbox]:checked").each(function () {
sum += parseInt($(this).val());
});
$('.qty').keyup(function () {
$('.qty').each(function () {
qty += parseInt(this.value, 10);
});
});
$('#total').val(sum * qty);
}
});
<input type="checkbox" id="1" value="30" name="1" />
<input type="checkbox" id="2" value="60" name="2" />
<input type="checkbox" id="3" value="90" name="3" />
<div style="display:none;" id="checked-1">
<input type="text" name="product_1_qty" id="product_1_qty" placeholder="Quantity" class=" qty" value="0" />
</div>
http://jsfiddle.net/isherwood/9vRtJ/1/
You are trying to set an element value with total ID as a final result, but there is no such element in your html code. Just add this element in your html code (here i put another textbox):
<input type="text" id='total'>
Check out the Live demo for your fix
Or you can use better coding:(Read the comments) - Working DEMO
$(document).ready(function () {
var sum = 0;
var qty = 0;
$("input[type=checkbox]").change(function () {
if ($('#1').is(':checked')) {
$('#checked-1').show();
} else {
$('#checked-1').hide();
}
});
$('.qty').keyup(function () { // if user typed anyting in the Quantity
sum = parseInt($('#1').val()); // get checkbox value and change the data type to int
qty = parseInt($(this).val()); // get Quantity value and change the data type to int
//console.log(sum,qty);
if(sum && qty) { // if the values was not empty
$('#total').val(sum * qty); // show the result in the total element
} else { // if the values was empty
$('#total').val(''); // clear the result textbox
}
});
});

Getting NaN from form PHP form using JS

I'm getting a NaN value if i try to do any math with the two vars i pull form the form. It also gives me NaN if i try it parseInt it. IDK if it helps but the values are pulled from the URL using PHP; example: .../serch.php?animal=all&color=any&sunSd=all&lifeSpn=all&limiterF=5&limiterT=20
limiterF and limiterT are the vars I'm working with.
html:
<form id='serchForm' action="serch.php" method="GET">
...
<fieldset>
From: <input type='text' id = 'limiterFid' name='limiterF' value=<?php if (!empty($_GET['limiterF'])) {echo $limiterF;} else {echo 0;} ?> size="2" /><br />
To: <input type='text' id = 'limiterTid' name='limiterT' value=<?php if (!empty($_GET['limiterT'])) {echo $limiterT;} else {echo 20;} ?> size="2" />
</fieldset>
<input type="submit" id="submitNew" />
<input type="submit" id="nextSerch" />
JS:
$(document).ready(function() {
$("#serchForm input").click(function(e) {
if(e.target.id == 'submitNew') {
e.preventDefault();
$('#limiterFid').attr("value", 0);
$('#limiterTid').attr("value", 20);
$("#serchForm").submit();
} else if (e.target.id == 'nextSerch') {
e.preventDefault();
var limiterF = $('#limiterFid').value,
limiterT = $('#limiterTid').value;
limiterT = limiterF + limiterT;
limiterF = parseInt(limiterF, 5);
$('#limiterFid').attr("value", limiterF);
$('#limiterTid').attr("value", limiterT);
$("#serchForm").submit();
} else {
return;
}
});
});
This is what it will return when you click nextSerch:
.../serch.php?animal=all&color=any&sunSd=all&lifeSpn=all&limiterF=NaN&limiterT=NaN
value is a property of the DOM element not the jQuery object. You can use either val() or value on the original DOM element.
$('element').val()
// OR
$('element')[0].value
Also to set values you typically don't use attr, you use val.
$('element').val('newValue')
Use the .val() method, as value will returned undefined as it is attempting to call the 'value' property of a jQuery object:
$(document).ready(function() {
$("#serchForm input").click(function(e) {
if(e.target.id == 'submitNew') {
e.preventDefault();
$('#limiterFid').val(0);
$('#limiterTid').val(20);
$("#serchForm").submit();
} else if (e.target.id == 'nextSerch') {
e.preventDefault();
var limiterF = $('#limiterFid').val(),
limiterT = $('#limiterTid').val();
limiterT = limiterF + limiterT;
limiterF = parseInt(limiterF, 5);
$('#limiterFid').val(limiterF);
$('#limiterTid').val(limiterT);
$("#serchForm").submit();
} else {
return;
}
});
});

Categories