开发者

Javascript locating hidden field

开发者 https://www.devze.com 2023-02-18 17:24 出处:网络
I have a table with an arbitrary number of rows. In the first cell in each table is an image with an onclick call to a javascript function, and a hidden field with the ID of the row. The call to the j

I have a table with an arbitrary number of rows. In the first cell in each table is an image with an onclick call to a javascript function, and a hidden field with the ID of the row. The call to the javascript function looks like:

<td>
    <a href="#" onclick="doSomeAction(this); return false;" title="Do Something">
        <img src="<?php echo($this->baseUrl());?>/images/btn_add.png" width="20" height="20" alt="Add" />
    </a>
    <input type="hidden" name="rowid[]" value="123" />
</td>

In my Javascript, I would like to retrieve the v开发者_高级运维alue of the id[] field. I tried to access it as follows:

var x = obj.parentNode.childNodes;
var i,j = x.length;
for ( i=0; i<j; i++ ) {
    if ( 'rowid[]' == x[i].nodeName.toLowerCase() ) {
        inp.value = x[i].value;
    } else {
        inp.value = 999;
    }
}

In all cases, this returns 999 as the value, indicating that it cannot locate the child node. What is not being done correctly here?


x[i].nodeName is the name of the element, e.g. INPUT, A, TD, DIV, etc. If you want to keep down this path, just check .name or .getAttribute('name')

Also, x should probably be obj.parentNode.parentNode.childNodes since the parentNode is the A tag.


If the following block appears in a loop you could assign ids to all html elements and then IMHO, you just need to pass that ID to ur JS method
while a loop runs{ <td id="the-tr-$id"> <a id="the-tr-href-$id" href="#" onclick="doSomeAction(this, $id); return false;" title="Do Something"> <img id="the-tr-href-img-$id" src="<?php echo($this->baseUrl());?>/images/btn_add.png" width="20" height="20" alt="Add" /> </a> <input type="hidden" name="rowid[]" id="the-tr-input-$id" value="123" /> </td> }

and in your method, you can do a document.getElementById()

I would recommend assigning IDs to all html elements, if it seems too much then important elements do need IDs, always helps.


alwaysFalse = 'rowID[]' == anyString.toLowerCase()
0

精彩评论

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