简体   繁体   English

如何使用JavaScript在动态表中动态添加下拉选择

[英]how to i add dynamically drop down select in dynamically table using javascript

how to i add dynamically drop down select in dynamically table using javascript but i want to use this select in the cell6.innerHTML= instead of input type="text" ? 如何使用javascript在动态表中动态添加下拉选择,但我想在cell6.innerHTML=使用此选择,而不是input type="text"

<script type="text/javascript">
var i = 0;
function changeIt(){
i++;
var table=document.getElementById("itemdetail");
var row=table.insertRow();
var cell4=row.insertCell();
var cell3=row.insertCell();
var cell1=row.insertCell();
var cell7=row.insertCell();
var cell5=row.insertCell();
var cell6=row.insertCell();

cell6.innerHTML="<input style='width:104px' 
type='text' readonly='readonly' name='itemcode[]' id='itemcode"+i+"' />";

cell5.innerHTML="<input  type='text' 
name='particulars[]' id='particulars"+i+"'/>";

cell7.innerHTML="<textarea class='textarea5' 
name='description[]' id='description"+i+"'></textarea>";

cell1.innerHTML="<input style='width:69px' 
type='text' name='qty[]' id='qty_"+i+"' onfocus='return B("+i+");' />";

cell3.innerHTML="<input style='width:50px'
type='text' name='rates[]' id='rates_"+i+"'/>";

cell4.innerHTML="<input style='width:80px' type='text'
readonly='readonly' name='amt[]'  id='amt_"+i+"'
onfocus='return B("+i+");' onMouseOver='return B("+i+");' />";
}
</script>    

i want to use this select in the cell6.innerHTML= instead of input type="text" ? 我想在cell6.innerHTML=使用此选择,而不是input type="text"吗?

<select class="select10" name="itemcode[]" id="itemcode" 
style="width:150px;"  onChange="getComboB(this)">
<option value="0"><--Select--> </option>
<?php 
$query=mysql_query("SELECT * from products order by id");
while($row=mysql_fetch_assoc($query))
{
$val2=$row['itemcode'];
?>
<option  value="<?=$val2;?>" <? if ($_GET['itemcode']  == $val2) 
{ echo "selected='selected'"; }?>>
<?=$row['itemcode'];?>
</option>
<?php }?>
</select>

Now i added this code instead of input type text on cell6.innerHTML= but problem is that new table row is not displaying? 现在,我在cell6.innerHTML=上添加了此代码,而不是input type text ,但是问题是new table row is not displaying?

cell6.innerHTML= '
<select class="select10" name="itemcode[]" id="itemcode" 
style="width:150px;"  onChange="getComboB(this)">
<option value="0"><--Select--> </option>
<?php 
$query=mysql_query("SELECT * from products order by id");
while($row=mysql_fetch_assoc($query))
{
$val2=$row["itemcode"];
?>
<option  value="<?=$val2;?>" <? if ($_GET['itemcode']  == $val2) 
{ echo "selected=\"selected\""; }?>>
<?=$row["itemcode"];?>
</option>
<?php }?>
</select>';

You try with Jquery. 您尝试使用Jquery。

If cell6 is a id, 如果cell6是id,

document.getElementById("cell").innerHTML+=" your code"

Exactly this: 正是这样:

document.getElementById("cell").innerHTML+="<select class="select10" name="itemcode[]" id="itemcode" 
style="width:150px;"  onChange="getComboB(this)">
<option value="0"><--Select--> </option>
<?php 
$query=mysql_query("SELECT * from products order by id");
while($row=mysql_fetch_assoc($query))
{
$val2=$row["itemcode"];
?>
<option  value="<?=$val2;?>" <? if ($_GET["itemcode"]  == $val2) 
{ echo "selected=\"selected\""; }?>>
<?=$row["itemcode"];?>
</option>
<?php }?>
</select>';"

Try this: 尝试这个:

cell6.innerHTML= '
<select class="select10" name="itemcode[]" id="itemcode" 
style="width:150px;"  onChange="getComboB(this)">
<option value="0"><--Select--> </option>
<?php 
$query=mysql_query("SELECT * from products order by id");
while($row=mysql_fetch_assoc($query))
{
$val2=$row["itemcode"];
?>
<option  value="<?=$val2;?>" <? if ($_GET["itemcode"]  == $val2) 
{ echo "selected=\"selected\""; }?>>
<?=$row["itemcode"];?>
</option>
<?php }?>
</select>';

But it is not good idea to get data directly into the view (If you are using MVC programming). 但是将数据直接放入视图中并不是一个好主意(如果您使用的是MVC编程)。 Instead of that you should get the data in the controller from the model, and then pass it to the view. 取而代之的是,您应该从模型中获取控制器中的数据,然后将其传递给视图。

Note double quotes in the js string. 注意js字符串中的双引号。 you have to have only 2 single quotes in the begin and at the end of the js. 您只需在js的开头和结尾加上2个单引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM