jquery 动态添加行绑定init事件

jquery 动态添加行绑定init事件,第1张

jQuery的live函数 是用于绑定事件委托的

什么是委托呢? 就是说 把这个事件交给jQuery 由jQuery来负责帮你绑定事件 当被指定绑定的元素增加或删除时,jQuery都会自动的绑定或解除此事件

使用方法:

$("选择元素").live("事件名",事件函数)

示例:

$("tr").live('click',function(){

//函数体

})

这样 当在以后页面动态加入了tr标签后 jQuery会自动为你绑定这个click事件

如果你是自定义事件 只要你是按照jQuery自定义事件定义规则定义的 那么你只需要将live第一个参数改为你的事件名即可

你的 dgv_CellDoubleClick 函数这样:

void dgv_CellDoubleClick(object sender, DataGridViewCellEventArgs e)

{

    DataGridView tempGdv =sender as DataGridView//获取事件发送者

    if(e.RowIndex>-1&&e.ColumnIndex>-1)//防止 Index 出错

    {

        String tempStr=tempGdv.Rows[e.RowIndex].Cells[0].Value.ToString() 

    }  

}

<html>

<head>

<title>Untitled Document</title>

<script>

function addRow()

{

var table = document.getElementById("t")

var tr = table.insertRow()

var td1 = tr.insertCell()

var input1 = document.createElement("input")

input1.name = "a"

input1.value = "a"

td1.appendChild(input1)

var td2 = tr.insertCell()

var input2 = document.createElement("input")

input2.name = "b"

input2.value = "b"

td2.appendChild(input2)

var td3 = tr.insertCell()

var input3 = document.createElement("input")

input3.name = "c"

input3.value = "c"

td3.appendChild(input3)

tr.ondblclick = function()

{

tr.parentNode.removeChild(tr)

}

}

</script>

</head>

<body>

<table id="t" border="1">

</table><br>

<input type="button" value="addRow" onclick="addRow()"/>

</body>

</html>

给你参考下


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11948333.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存