jquery attr("onclick",function()) 自动触发onclick事件生成行,导致死循环

luoshiyindfj 2011-09-07 08:53:26

function createRowItem(rowId)
{
var rows=$("#tab_OrderDetail tr");
var rowLen=rows.length;
var rowN= rowId+1;
if(rowN==rowLen)
{
var tr=$("<tr>").append("<td>"+rowN+"</td><td><input type=\"text\" id=\"truckName_"+rowN+"\"/></td><td><input type=\"text\" id=\"truckCode_"+rowN+"\"/></td><td><select id=\"truckType_"+rowN+"\"><option >0</option></select></td><td><input type=\"text\" id=\"configuration_"+rowN+"\"/></td><td><input type=\"text\" id=\"price_"+rowN+"\"/></td><td><input type=\"text\" id=\"Amount_"+rowN+"\"/></td><td><input type=\"text\" id=\"totalMoney_"+rowN+"\"/></td><td><select id=\"consignType_"+rowN+"\"><option>0</option></select></td><td><input type=\"text\" id=\"consignTime_"+rowN+"\"/></td>");
tr.attr("id", (rowN));
$("#tab_OrderDetail").append(tr);
$("#consignTime_"+rowN+"").attr("onclick",createRowItem(rowN));
}else{ return;}
}

...全文
882 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
fengyun817 2011-09-07
  • 打赏
  • 举报
回复
封装一下就行,function(){createRowItem(rowN)}

<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
//<![CDATA[
$(function(){
$("#row_1").one("click",function(){addRow($(this).attr("id"))});
});

function addRow(rowid)
{
var nextRowid = "row_"+(rowid.split("_")[1]+1);
$($("#"+rowid).clone()).appendTo($("#test")).attr("id",nextRowid).one("click",function(){ addRow(nextRowid)});;
}
//]]>
</script>

<body>
<table id="test">
<tr id="row_1">
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
</table>
</body>
luoshiyindfj 2011-09-07
  • 打赏
  • 举报
回复
奇怪,写在 html里面 onclick=\"createRowItem("+rowN+");\" 就OK了。 再琢磨琢磨。谢谢了。
三石-gary 2011-09-07
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 luoshiyindfj 的回复:]

确实是这样,点击第一行之后,直接就循环的生成行,onclick事件没起到控制生成的作用。直接就触发了。有什么高见没有?
[/Quote]

想办法不放在里面也能实现就行了
luoshiyindfj 2011-09-07
  • 打赏
  • 举报
回复
确实是这样,点击第一行之后,直接就循环的生成行,onclick事件没起到控制生成的作用。直接就触发了。有什么高见没有?
三石-gary 2011-09-07
  • 打赏
  • 举报
回复
$("#consignTime_"+rowN+"").attr("onclick",createRowItem(rowN));只要点击就会造成死循环
showenxxx 2011-09-07
  • 打赏
  • 举报
回复
jquery里面事件绑定可以用
$("input").click(function(){
//do stuff
});

$("input").bind("click",function(){
//do stuff
});

$("input").live("click",function(){
//do stuff
});

52,797

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧