开发者

Add value and id dynamically to an Input type hidden

I have this bit of code which I am not sure if it is possible what I need to do, if you check the input type 'hidden', you will see this (id="'inpTdId'+ tdIndex" value="tdRegularMedicine_+ inpTdId") as I need to get the input hidden to store a new value and id for each time the loop goes through.

I am trying to generate a new element each time it goes through the loop, with a new id and a new value.

I need to do this so I can store the id of another element as value of the hidden item, in order to be able to display and focus that element later on.

Is it possible to do this"?

Code eg(I might have some mistakes on code but I have done it quick, just for you to get and Idea, I apologise if I have any)

<script type="text/javascript">
var tdIndex = 0;
</script>
for (x=0;x<noOfObj;x++{
<script type="text/javascript">
    var b = ""
    b = "tdRegularMedicine_" + tdIndex;
</script>
    <tr>
    <td align="left" style="padding-left:2px;">
    <input type="hidden" id="'in开发者_StackOverflow社区pTdId'+ tdIndex" value="tdRegularMedicine_+ inpTdId" />
    </td>
        </tr>
<script type="text/javascript">
    var obj = document.getElementById("inpTdId"+tdIndex);
    alert(obj.value);
    tdIndex++;
</script>
}

Thanks.


If you want to generate html using javascript, then you either need to use innerHTML or DOM. For example:

<div id="myDiv">

</div>
<script type="text/javascript">
var myDiv = document.getElementById("myDiv");
for(var i=0; i<10; i++){
  myDiv.innerHTML += "<input type=\"hidden\" value=\"tdVal"+i+"\" />";
}
</script>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜