PDA

View Full Version : محاسبه مقادیر درون آرایه ای از TextBox ها



maa_biz
پنج شنبه 29 فروردین 1387, 00:15 صبح
دوستان من می خواستم مقادیری رو که درون هر یک از Text Box ها هست با جاوا اسکرپیت جمع بزنم اولا برای تبدیل string به number از چی استفاده کنم در ثانی برای چاپ این مقادیر از alert استفاده می کنم مثل کد زیر :
<Form name=frm>
<Input type=text name=var1 value=1>
<Input type=text name=var2 value=2>
<Input type=text name=var3 value=3>
<Input type=text name=var4 value=4>
<Input type=button value=Sum onclick="alert(document.frm.var1.value);">
</Form>ولی مشکل اصلی من اینه که نام تگها را به صورت آرایه تعریف کردم (تو php ازش استفاده می کنم)
یعنی به این شکل :
<Form name=frm>
<Input type=text name=var[1] value=1>
<Input type=text name=var[2] value=2>
<Input type=text name=var[3] value=3>
<Input type=text name=var[4] value=4>
<Input type=button value=Sum onclick="alert(document.frm.var[1].value);">
</Form>و حالا دیگه جواب نمیده, چطوری بنویسمش؟
متشکرم.

tabib_m
پنج شنبه 29 فروردین 1387, 14:17 عصر
1. از parseFloat استفاده کن:


theInt=parseFloat(document.getElementById['theID'].value);


2. به جای این که با اسم فرم ها و فیلدها بازی کنی، از id استفاده کن:


<Form name="frm">
<Input type="text" name="var[1]" value="1" id="var1">
<Input type="text" name="var[2]" value="2" id="var2">
<Input type="text" name="var[3]" value="3" id="var3">
<Input type="text" name="var[4]" value="4" id="var4">
<Input type=button value=Sum onclick="alert(document.getElementById('var1').value);">
</Form>


موفق باشید.