توجه ! این یک نسخه آرشیو شده میباشد و در این حالت شما عکسی را مشاهده نمیکنید برای مشاهده کامل متن و عکسها بر روی لینک مقابل کلیک کنید : راهنمایی در مورد یک مشکل بسیار جزئی
من میخوام مقادیر j1 و j2 رو با هم جمع کنم
مثلا عدد: 5 - 4 میشه مقدارش 9
ولی با این کد میشه 45
میشه راهنمایی کنید؟
::::
مورد دوم در مورد OR هست
من میخوام چند تا فیلد با هم جمع بشن مثلا 4 تا فیلد
با کد زیر: وقتی هر 4 مقدار پر بشه جمع میشه، که من میخوام هر چقدر از مقادیر که پر هست با هم جمع بشن
یعنی توی شرط if به جای && فکر کنم باید از OR استفاده بشه؛ نه؟
سلام
1- به علتی که 4 و 5 رشته در نظر گرفته میشه و شما عملا دو رشته رو به هم می چسبونید یعنی 4+5=45
برای حل این مشکل باید رشته رو به عدد تبدیل کنید با دستور parseFloat(عدد اعشاری) یا (عددصحیح)parseInt
2-بله باید or استفاده کنید فقط یک نکته اگر تکست خالی باشه undefined بر میگردونه که باعث خطا میشه شما بیا اول چک کن که تکست اگر خالی بود عدد 0 توش قرار بگیره تا مشکل پیش نیاد
چون 0 در جمع اثر نداره
aghaee
February 3rd, 2011, 15:44
میشه نمونه کدش رو بدین چون من آشنایی ندارم به جاوا
ممنون میشم
the_coder
February 3rd, 2011, 16:02
a=document.getElementById('j1').value;
b=document.getElementById('j2').value;
c=parseInt(a)+parseInt(b);
c نتیجه ای است که شما می خواین
aghaee
February 3rd, 2011, 16:07
مشکل حلقه if چی؟ چطوری or کنم؟
یعنی چند تا فیلده که میخوام با هم جمع بشن، توی دستور داده شده تا زمانی که 4 تمامی فیلد ها پر نشه جواب رو نمیده میخوام وقتی یکی یا دوتا فیلد پر شد نتیجه رو بده
a=document.getElementById('j1').value;
b=document.getElementById('j2').value;
c=document.getElementById('j3').value;
d=document.getElementById('j4').value;
if(is_int(a)) a=0;
if(is_int(b)) b=0;
if(is_int(c)) c=0;
if(is_int(d)) d=0;
e=parseInt(a)+parseInt(b)+parseInt(c)+parseInt(d); e نتیجه شماست
البته اگر اعداد شما ممکنه اعداد اعشاری باشه جای parseInt از parseFloat استفاده کنید
aghaee
February 3rd, 2011, 16:31
تو این حالت وقتی یکی از مقادیر خالی هست NaN میده خروجی رو :106:
if(is_int(document.getElementById('j1').value) || is_int(document.getElementById('j2').value) || is_int(document.getElementById('j3').value)) document.getElementById('gimatefactor').value = a + b + c ;
}
function is_int(value){
if((parseFloat(value) == parseInt(value)) && !isNaN(value)){
return true;
} else {
return false;
}
}
</script>
[/QUOTE]
در فانکشن is_int جای true و false رو با هم عوض کنید مشکلتون حل میشه
اگر باز نتونستید سورس رو برام پ . خ کنید تا درستش کنم
aghaee
February 3rd, 2011, 17:35
نتیجه رو اینطوری جمع می کنه 4 + 5 میشه 45
---------- Post added at 05:35 PM ---------- Previous post was at 05:16 PM ----------