$('#<%=DropDownList2.ClientID %>').change(function () {
if ($(this).val() == "inclusive") {
parseFloat($("#Textbox3").val($("#Textbox1").val() * $("#Textbox2").val()));
// var main = parseFloat($('#Textbox3').val());
// var disc = parseFloat($('#Textbox4').val());
//var dec = (disc / 100).toFixed(2);
//var mult = main * dec;
//var discont = main + mult;
//$('#Textbox3').val(discont);
}
else if ($(this).val() == "Exclusive") {
parseFloat($("#Textbox3").val($("#Textbox1").val() * $("#Textbox2").val()));
var main = parseFloat($('#Textbox3').val());
var disc = parseFloat($('#DropDownList1').val(18));
var dec = (disc / 100).toFixed(2);
var mult = main * dec;
var discont = main + mult;
i get textbox3 returns
nan value when select item from dropdownlist1
Looks like You were calling parseFloat at some places which is not needed. Try with below code
<script>
$(document).ready(function () {
$("#Textbox1, #Textbox2").keyup(function () {
$("#Textbox3").val($("#Textbox1").val() * $("#Textbox2").val());
});
$('#<%=DropDownList2.ClientID %>').change(function () {
if ($(this).val() == "inclusive") {
//Removed parsefloat
$("#Textbox3").val($("#Textbox1").val() * $("#Textbox2").val());
}
else if ($(this).val() == "Exclusive") {
//Removed parsefloat
$("#Textbox3").val($("#Textbox1").val() * $("#Textbox2").val());
var main = parseFloat($('#Textbox3').val());
//Removed parsefloat
//assigned the value 18 to dropdownlist
$('#DropDownList1').val(18);
//Get the selected item from Dropdownlist
var disc = $("#DropDownList1").children("option:selected").val();
var dec = (disc / 100).toFixed(2);
var mult = main * dec;
var discont = main + mult;
$('#Textbox3').val(discont);
}
});
});
</script>
[jQuery.val] checks, or selects, all the radio buttons, checkboxes, and select options that match the set of values.
This behavior is injQueryversions1.2and above.
However, in the given select element, I can only see that you have assigned two options
'18' and '12', not 18 in number. Therefore, when you assign the value
18 to the select, it cannot match this value hence fail.
I suggest you provide us with a simple demo which could explain the problem.
This would be much helpful for us targeting and solving the problem easier and faster.
Thank you for understanding.
Best regards,
Sean
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
$('#<%=DropDownList1.ClientID %>').change(function () {
if ($(this).val() == "12") {
parseFloat($("#Textbox3").val($("#Textbox1").val() * $("#Textbox2").val()));
var main = parseFloat($('#Textbox3').val());
var disc = parseFloat($('#Textbox4').val());
var dec = (disc / 100).toFixed(2);
var mult = main * dec;
var discont = main + mult;
$('#Textbox3').val(discont);
}
else if ($(this).val() == "18") {
parseFloat($("#Textbox3").val($("#Textbox1").val() * $("#Textbox2").val()));
var main = parseFloat($('#Textbox3').val());
var disc = parseFloat($('#Textbox4').val());
var dec = (disc / 100).toFixed(2);
var mult = main * dec;
var discont = main + mult;
I added DropDownList1.ClientID change event handler directly to my above codes and the result is that the codes are working though the two if conditions return the same result.
I suggest you could set print console points in the codes and press F12 to open the dev tools in browsers to check whether the functions are called or not.
For example,
$('#<%=DropDownList1.ClientID %>').change(function () {
console.log($(this).val());
if ($(this).val() == "12") {
// If this if condition met, 'DDL1 is 12' will be printed. Below codes are in the same check principle.
console.log("DDL1 is 12");
$("#Textbox3").val($("#Textbox1").val() * $("#Textbox2").val());
var main = parseFloat($('#Textbox3').val());
var disc = parseFloat($('#Textbox4').val());
var dec = (disc / 100).toFixed(2);
var mult = main * dec;
var discont = main + mult;
// Print values
console.log("main value: " + main);
console.log("disc value: "+disc);
console.log("dec value: " + dec);
console.log("mult value: " + mult);
console.log("discont value: " + discont);
$('#Textbox3').val(discont);
}
else if ($(this).val() == "18") {
console.log("DDL1 is 18");
$("#Textbox3").val($("#Textbox1").val() * $("#Textbox2").val());
var main = parseFloat($('#Textbox3').val());
var disc = parseFloat($('#Textbox4').val());
var dec = (disc / 100).toFixed(2);
var mult = main * dec;
var discont = main + mult;
console.log("main value: " + main);
console.log("disc value: " + disc);
console.log("dec value: " + dec);
console.log("mult value: " + mult);
console.log("discont value: " + discont);
$('#Textbox3').val(discont);
}
});
Console result in browser (Mine is Chrome):
Obviously, if there are some lines in the console section of the dev-tool of the browser, it means that the functions are working.
You could still change the console function's position to check any place of the codes.
Best regards,
Sean
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
55 Points
191 Posts
when select dropdownlist item this returns nan value in jquery?
Sep 08, 2020 12:31 PM|prabhjot1313|LINK
hello in my script i want to add dropdownlist1 and i get textbox3 returns nan value when select item from dropdownlist1
here is script
<script>
$(document).ready(function () {
$("#Textbox1, #Textbox2").keyup(function () {
$("#Textbox3").val($("#Textbox1").val() * $("#Textbox2").val());
});
$('#<%=DropDownList2.ClientID %>').change(function () {
if ($(this).val() == "inclusive") {
parseFloat($("#Textbox3").val($("#Textbox1").val() * $("#Textbox2").val()));
// var main = parseFloat($('#Textbox3').val());
// var disc = parseFloat($('#Textbox4').val());
//var dec = (disc / 100).toFixed(2);
//var mult = main * dec;
//var discont = main + mult;
//$('#Textbox3').val(discont);
}
else if ($(this).val() == "Exclusive") {
parseFloat($("#Textbox3").val($("#Textbox1").val() * $("#Textbox2").val()));
var main = parseFloat($('#Textbox3').val());
var disc = parseFloat($('#DropDownList1').val(18));
var dec = (disc / 100).toFixed(2);
var mult = main * dec;
var discont = main + mult;
$('#Textbox3').val(discont);
}
});
});
</script>
All-Star
50841 Points
9895 Posts
Re: when select dropdownlist item this returns nan value in jquery?
Sep 08, 2020 01:22 PM|A2H|LINK
Looks like You were calling parseFloat at some places which is not needed. Try with below code
Aje
My Blog | Dotnet Funda
All-Star
58214 Points
15668 Posts
Re: when select dropdownlist item this returns nan value in jquery?
Sep 08, 2020 02:18 PM|bruce (sqlwork.com)|LINK
When you set a .val() like below
var disc = parseFloat($('#DropDownList1').val(18));
val(18) returns a jquery object not the value, so parseFloat return nan.
Member
55 Points
191 Posts
Re: when select dropdownlist item this returns nan value in jquery?
Sep 09, 2020 06:46 AM|prabhjot1313|LINK
Contributor
2890 Points
848 Posts
Re: when select dropdownlist item this returns nan value in jquery?
Sep 11, 2020 06:47 AM|Sean Fang|LINK
Hi prabhjot1313,
Now I think a better way to solve your problem is that we should firstly specify the problem.
The problem from your description is that you can not assign the value '18' to the select element (Drop down list), correct?
If so, I think you need to change the line of code from
to
The reason is that jQuery's documentation states:
This behavior is in
jQuery
versions1.2
and above.However, in the given select element, I can only see that you have assigned two options '18' and '12', not 18 in number. Therefore, when you assign the value 18 to the select, it cannot match this value hence fail.
I suggest you provide us with a simple demo which could explain the problem.
This would be much helpful for us targeting and solving the problem easier and faster.
Thank you for understanding.
Best regards,
Sean
Member
55 Points
191 Posts
Re: when select dropdownlist item this returns nan value in jquery?
Sep 14, 2020 07:25 AM|prabhjot1313|LINK
sir when i apply this code '18' the result is not calculate but nan is not occur so solve them
<script>
$(document).ready(function () {
$("#Textbox1, #Textbox2").keyup(function () {
$("#Textbox3").val($("#Textbox1").val() * $("#Textbox2").val());
});
$('#<%=DropDownList1.ClientID %>').change(function () {
if ($(this).val() == "12") {
parseFloat($("#Textbox3").val($("#Textbox1").val() * $("#Textbox2").val()));
var main = parseFloat($('#Textbox3').val());
var disc = parseFloat($('#Textbox4').val());
var dec = (disc / 100).toFixed(2);
var mult = main * dec;
var discont = main + mult;
$('#Textbox3').val(discont);
}
else if ($(this).val() == "18") {
parseFloat($("#Textbox3").val($("#Textbox1").val() * $("#Textbox2").val()));
var main = parseFloat($('#Textbox3').val());
var disc = parseFloat($('#Textbox4').val());
var dec = (disc / 100).toFixed(2);
var mult = main * dec;
var discont = main + mult;
$('#Textbox3').val(discont);
}
});
});
</script>
Contributor
2890 Points
848 Posts
Re: when select dropdownlist item this returns nan value in jquery?
Sep 16, 2020 07:27 AM|Sean Fang|LINK
Hi prahjot1313,
What do you mean "the result is not calculate"?
I added DropDownList1.ClientID change event handler directly to my above codes and the result is that the codes are working though the two if conditions return the same result.
I suggest you could set print console points in the codes and press F12 to open the dev tools in browsers to check whether the functions are called or not.
For example,
Console result in browser (Mine is Chrome):
Obviously, if there are some lines in the console section of the dev-tool of the browser, it means that the functions are working.
You could still change the console function's position to check any place of the codes.
Best regards,
Sean