PDA

توجه ! این یک نسخه آرشیو شده میباشد و در این حالت شما عکسی را مشاهده نمیکنید برای مشاهده کامل متن و عکسها بر روی لینک مقابل کلیک کنید : راهنمایی در مورد یک مشکل بسیار جزئی



aghaee
February 3rd, 2011, 13:21
سلام
کد:



document.getElementById('j1').value+document.getEl ementById('j2').value;

من میخوام مقادیر j1 و j2 رو با هم جمع کنم
مثلا عدد: 5 - 4 میشه مقدارش 9
ولی با این کد میشه 45

میشه راهنمایی کنید؟


::::

مورد دوم در مورد OR هست

من میخوام چند تا فیلد با هم جمع بشن مثلا 4 تا فیلد
با کد زیر: وقتی هر 4 مقدار پر بشه جمع میشه، که من میخوام هر چقدر از مقادیر که پر هست با هم جمع بشن
یعنی توی شرط if به جای && فکر کنم باید از OR استفاده بشه؛ نه؟

کد:



if(is_int(document.getElementById('j1').value) && is_int(document.getElementById('j2').value)) document.getElementById('gimatefactor').value = document.getElementById('j1').value+document.getEl ementById('j2').value;

ممنون میشم راهنمایی کنید

the_coder
February 3rd, 2011, 15:17
سلام
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 تمامی فیلد ها پر نشه جواب رو نمیده میخوام وقتی یکی یا دوتا فیلد پر شد نتیجه رو بده

ممنون میشم راهنمایی کنید

the_coder
February 3rd, 2011, 16:27
function is_int(value){
if((parseFloat(value) == parseInt(value)) && !isNaN(value)){
return false;
} else {
return true;
}
}

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:

the_coder
February 3rd, 2011, 16:40
مشکلی نداره - فایل پیوست رو دانلود کنید :

aghaee
February 3rd, 2011, 17:06
تو کد من جواب نمی ده میزنه NAN



<script>
function sum(){
if(is_int(document.getElementById('t1').value) && is_int(document.getElementById('f1').value)) document.getElementById('j1').value = document.getElementById('t1').value*document.getEl ementById('f1').value;
if(is_int(document.getElementById('t2').value) && is_int(document.getElementById('f2').value)) document.getElementById('j2').value = document.getElementById('t2').value*document.getEl ementById('f2').value;

if(is_int(document.getElementById('t3').value) && is_int(document.getElementById('f3').value)) document.getElementById('j3').value = document.getElementById('t3').value*document.getEl ementById('f3').value;

if(is_int(document.getElementById('t4').value) && is_int(document.getElementById('f4').value)) document.getElementById('j4').value = document.getElementById('t4').value*document.getEl ementById('f4').value;

if(is_int(document.getElementById('t5').value) && is_int(document.getElementById('f5').value)) document.getElementById('j5').value = document.getElementById('t5').value*document.getEl ementById('f5').value;



a=document.getElementById('j1').value;
b=document.getElementById('j2').value;
c=document.getElementById('j3').value;
d=document.getElementById('j4').value;
a=parseInt(a);
b=parseInt(b);
c=parseInt(c);
d=parseInt(d);
if(is_int(a)) a=0;
if(is_int(b)) b=0;
if(is_int(c)) c=0;
if(is_int(d)) d=0;

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>

the_coder
February 3rd, 2011, 17:10
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 ----------

سلام
مشکل با دستور زیر حل شد

a = Number (a);