2011-09-29 59 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> 
相关问题