简体   繁体   English

如何将参数传递给javascript函数

[英]how to pass parameter to javascript function

I have the following javascript. 我有以下javascript。

<script>
function getMethod(id){alert(id);}

</script>

following is html, 以下是html,

<table id="tbl1">
<tr>
<td><input type="button" onclick="getMethod()"/></td>
</tr>
</table>

I need to pass table id "tbl1" to javascript method getMethod on click event of html button. 我需要在html按钮的click事件上将表id“tbl1”传递给javascript方法getMethod。 so what should I do? 所以我该怎么做? what I want is something like this,(Passing table ID onclick method's parameters) 我想要的是这样的,(传递表ID onclick方法的参数)

<input type="button" onclick="getMethod('$("tbl1").ID')"/>

how can I do this? 我怎样才能做到这一点?

Thanks 谢谢

Don't pass anything rather than the this reference and do, 不要传递任何东西而不是this参考而且,

HTML HTML

<input type="button" onclick="getMethod(this)"/>

JS JS

function getMethod(elem){
   alert($(elem).closest('table').attr('id'));
}

In the above function we have used .closest() to grab the parent table. 在上面的函数中,我们使用.closest()来获取父表。 and we have used .attr('id') to retrieve its id. 我们使用.attr('id')来检索它的id。

DEMO DEMO

<input type="button" onclick="getMethod('tbl1')"/>

Update, since your comment made it clear this needs to be more 'dynamic' 更新,因为您的评论明确指出这需要更加“动态”

Here is a vanilla javascript implementation: 这是一个vanilla javascript实现:

function getMethod(element) {
        // element -> td -> tr -> tbody ->table
        parentTable = element.parentNode.parentNode.parentNode.parentNode;
        alert(parentTable.id);
    }

Called using : 叫做使用:

<input type="button" onclick="getMethod(this)" />

Demo: http://jsfiddle.net/robschmuecker/MxWR7/1/ 演示: http//jsfiddle.net/robschmuecker/MxWR7/1/

Pass only this : 只通过这个

<input type="button" onclick="getMethod(this)"/>

In your functuon write this: 在你的功能中写下这个:

<script>
    function getMethod(idget) {
        alert($(idget).closest("table").attr("id"));
    }
</script>

Since you tagged the question with jquery, You dont have to write inline code for click handler 由于您使用jquery标记了问题,因此您不必为单击处理程序编写内联代码

<table id="tbl1">
    <tr>
        <td>
            <input type="button" />
        </td>
    </tr>
</table>

jquery code will be jquery代码将是

$("#tbl1 button").click(function () {
    alert($(this).closest("table").attr("id"));
});

As a general rule, it is much better to add event listeners through javascript than inline. 作为一般规则,通过javascript添加事件侦听器要比内联更好。 So assuming you use jQuery on the page: 所以假设你在页面上使用jQuery:

$(document).ready(function() {
    //'#myButton' should be some jQuery identifier for your button
    $('#myButton').click(function(e) {
        getMethod('tbl1');
    });
});

Then in your html for the button: 然后在你的html按钮:

<input type="button" id="myButton">

And you're done! 而且你已经完成了! If you need some programmatic way to identify the parent of your element, look at [$.parents()]. 如果您需要某种编程方式来识别元素的父元素,请查看[$ .parents()]。 1 1

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

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