2011-09-29 58 views
0

我對javascript有疑問。是否有可能獲得具有相同名稱的文本框的值?是否可以獲得具有相同名稱的文本框的所有值?

例如

<INPUT TYPE="text" NAME="inputbox" VALUE=""> 
<INPUT TYPE="text" NAME="inputbox" VALUE=""> 

這些文本框具有相同的名稱,我怎樣才能通過名字來取得它的價值?

好吧,讓我回到真正的問題,我希望解釋正確。

我有一系列的文本框,我現在得到驗證。通常驗證文本框並不是什麼大事。

但是,這是一個文本框數組,其中文本框的ID只有在通過單擊+按鈕動態添加字段時纔可用。當點擊按鈕時,將出現整組文本框。儘可能多地點擊它,它會被添加。

所以它不可能獲得的值與ID驗證,所以我嘗試了名稱。

JavaScript函數

function insComep() 
    { 

    // $("#doctorPri").validationEngine("updatePromptsPosition") 
// jQuery("#doctorPri").validationEngine('attach', {promptPosition : "topRight"}); 

     rl=document.getElementById("compeTable").rows.length; 
     var a=document.getElementById("compeTable").insertRow(rl); 

     var g=a.insertCell(0); 
     var f=a.insertCell(1); 
     var m=a.insertCell(2); 
     var n=a.insertCell(3); 
     var o=a.insertCell(4); 
     var p=a.insertCell(5); 
     var q=a.insertCell(6); 
     var r=a.insertCell(7); 
     var s=a.insertCell(8); 
     var t=a.insertCell(9); 
     //var v=a.insertCel1l(11); 
     //var u=a.insertCell(12); 
g.innerHTML='<select name="competproduct[]" style="width:86px" id="competproduct'+rl+'" class="validate[required]" ><option value="">--Select--</option><?echo $product_str;?></select>'; 
f.innerHTML='<input type="text" name="competcompany[]" id="competcompany'+rl+'" size="10" class="validate[required]" >'; 
m.innerHTML='<input type="text" name="competbrand[]" id="competbrand'+rl+'" size="10" class="validate[required]" >'; 
n.innerHTML='<input type="text" name="competdrug[]" id="competdrug'+rl+'" size="10" class="validate[required]" >'; 
o.innerHTML='<input type="text" name="competquty[]" id="competquty'+rl+'" size="2" class="validate[required,custom[integer]] text-input" >'; 
p.innerHTML='<input type="text" name="competprice_frm[]" id="competprice_frm'+rl+'" size="2" class="validate[required,custom[number],funcCall[getPriceFromE]] text-input" />'; 
q.innerHTML='<input type="text" name="competprice_to[]" id="competprice_to'+rl+'" size="2" class="validate[required,custom[number],funcCall[getPriceTo]] text-input" />'; 
r.innerHTML='<input type="text" name="competmrp[]" id="competmrp'+rl+'" size="2" class="validate[required,custom[number],funcCall[getMrp]] text-input"/>'; 
s.innerHTML='<select name="ChemistParma[]" style="width:86px" id="ChemistParma'+rl+'" style="width:86px" ><option value="">--Select--</option><?echo $chemist_str;?></select>'; 
t.innerHTML='<img src="media/images/details_close.png" onClick="delCompe('+rl+'); "/>'; 
// jQuery("#doctorPri").validationEngine('attach', {promptPosition : "topRight"}); 


    $("#doctorPri").validationEngine('hideAll'); 
    } 

HTML

<table width="100%" id='compeTable' border='0' style='margin-left:auto;margin-right:auto;margin-top:40px;' > 


<tr style="border-bottom:1px solid #999999;"><td colspan='4'> 
<div style="background:;padding:3px;text-align:left; "> 
    <font color='black'><strong >Competitor Detail(s):</strong></font><font color="red">*</font> 
</div> 

</td></tr> 



     <tr> 
     <td>Product Name:<font color="red">*</font></td> 
<td>Company Name:<font color="red">*</font></td> 
<td>Brand Name:<font color="red">*</font></td> 
<td>Drug Name:<font color="red">*</font></td> 
<td> Quantity:<font color="red">*</font></td> 
<td>Pricefrom Dist:<font color="red">*</font></td> 
<td >Price to Dist:<font color="red">*</font></td> 
<td> MRP:<font color="red">*</font></td> 
<td>Chemist<font color="red">*</font><input type='button'value='+' style='width:1px' style='width:1px' onclick='frame5()'/> 
</td> 
<td></td> 
</tr> 
<tr><td>&nbsp;</td></tr> 


      <tr> 


        <td> 
        <select name='competproduct[]' id='competproduct' style="width:86px" class="validate[required]" > 
        <option value=''>-select Product-</option> 
        <? echo $product_str;?> 

        </select> 
       </td> 

       <td> 
        <input type="text" name="competcompany[]" id="competcompany" size="10" class="validate[required]" > 
       </td> 
       <td ><input type="text" name="competbrand[]" id="competbrand" size="10" class="validate[required]" > 
       </td> 
       <td><input type="text" name="competdrug[]" id="competdrug" size="10" class="validate[required]" > 
       </td> 
       <td><input type="text" name="competquty[]" id="competquty" size="2" class="validate[required,custom[integer]] text-input" > 
       </td> 
       <td> 
<input type="text" name="competprice_frm[]" id="competprice_frm" size="2" class="validate[required,custom[number],funcCall[getPriceFromE]] text-input" /> 

       </td> 
       <td> 
       <input type="text" name="competprice_to[]" id="competprice_to" size="2" class="validate[required,custom[number],funcCall[getPriceTo]] text-input" /> 
       </td> 
       <td><input type="text" name="competmrp[]" id="competmrp" size="2" class="validate[required,custom[number],funcCall[getMrp]] text-input" onBlur=''/> 
       </td> 
<td> 
<select name='ChemistParma[]' id='ChemistParma' style="width:86px"> 
<option value=''>-select chemict-</option> 
<?echo $chemist_str?> 
</select></td> 
<td> 
<img src="media/images/details_open.png" onClick="insComep()"/> 
</td> 

       </tr> 
     </table> 
+0

這是非常簡單的使用jQuery和我可以張貼一個例子,但首先我需要知道你如果使用jQuery是一個選項,或者你是否需要純粹的JavaScript而沒有任何額外的框架。編輯:下面發佈了一個非框架答案,但毆打它:) – SpaceBison

+0

我認爲你的HTML標籤沒有正確關閉這裏要麼btw,我認爲你需要<輸入.... /> – SpaceBison

+0

在這裏,我只是想驗證價格要遠遠不應該是MRP。我現在怎麼做 –

回答

3

這是很簡單的,

document.getElementsByName("inputBox"); 
+0

我不認爲這會有所幫助,因爲它只用於複選框和單選按鈕。但不包含文本框。 –

+0

這對我相信的文本框有效。 -SB – SpaceBison

+0

@ Bala.C - 帶名稱的輸入是帶名稱的輸入。無論它是複選框還是文本輸入都無關緊要。你得到一個NodeList並循環它。唯一的區別是你只關心它是否有值,如果它是文本輸入的話(如果它是一個複選框,你也會關心它是否是'.checked')。 – Quentin

2

您可以使用不同的方法,衆說紛紜。

1)手動遍歷childNodes等,並檢查nodeName。這很相關,需要很多無聊的代碼,所以我不會在這裏寫一個例子。

2)document.getElementsByTagName。這也可以在DOM節點上使用,因此您可以執行類似document.getElementById("my_form").getElementsByTagName("input")的操作,具體取決於DOM的其他部分。

3)​​。字符串參數是一個完整的CSS選擇器。一些舊的瀏覽器不支持這個。

+0

+1比我更詳細:) – SpaceBison

1

這裏例如爲您與您的代碼:

<div id="inputs"> 
    <INPUT TYPE="text" NAME="inputbox" VALUE="asd"> 
    <INPUT TYPE="text" NAME="inputbox" VALUE="efs"> 
</div> 

<script> 
    $(document).ready(function() { 
     var howmany = $('#inputs').children("input").length; 
     alert(howmany); 

     for(i=0; i<= howmany-1; i++) { 
      var input = $("#inputs").children("input").eq(i).attr('VALUE'); 
      alert(input); 
     } 
    }); 
</script> 
相關問題