开发者

Give the JavaScript the button's id

In my JavaScript code, I have to make a line like this (I use smarty template engine, that is the literal stuff).

<script language="javascript" type="text/javascript">
    function ajaxTextKill() {
        // ...etc.
        ajaxRequest.open("GET", "functions.php?action=kill{/literal}&id="+IWANTMYIDHERE+"&p={$smarty.get.page}&c={$smarty.get.sel}{literal}", true);
        ajaxRequest.send(null);
    }

After this in my HTML code,

<input type="button" id="87" value="del" onClick="return ajax开发者_如何学PythonTextKill();" />

I'd like to give the JavaScript the input's id value. How to do this?


You don't necessarily need the ID if you pass a reference to the field itself.

<input type="button" id="87" value="del" onClick="return ajaxTextKill(this);" />

And access the ID like so:

function ajaxTextKill(object){
    alert(object.id);
}


<input type="button" id="a87" value="del" onClick="return ajaxTextKill(this.id);" />


The HTML

<input type="button" id="87" value="del" onClick="return ajaxTextKill(this.id);" />

The JavaScript

function ajaxTextKill(id){
...etc.
ajaxRequest.open("GET", "functions.php?action=kill{/literal}&id="+id+"&p={$smarty.get.page}&c={$smarty.get.sel}{literal}", true);
ajaxRequest.send(null); }


Let the element pass itself to the function by ajaxTextKill(this). Then just grab its ID by element.id.

function ajaxTextKill(element) {
    var buttonid = element.id;
    ajaxRequest.open("GET", "functions.php?action=kill{/literal}&id="+ buttonid +"&p={$smarty.get.page}&c={$smarty.get.sel}{literal}", true);
    ajaxRequest.send(null);
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜