如何更新';onclick';jquery中的事件?
我有一个看起来应该有用的方法,但实际上不行如何更新';onclick';jquery中的事件?,jquery,Jquery,我有一个看起来应该有用的方法,但实际上不行 function copyform(curnum){ if(curnum == 3) $('p').html('Thank You!') var num = $('form').length; var newNum = new Number(num + 1); // create the new element via clone(), and manipulate its ID using newNum value
function copyform(curnum){
if(curnum == 3) $('p').html('Thank You!')
var num = $('form').length;
var newNum = new Number(num + 1);
// create the new element via clone(), and manipulate its ID using newNum value
var newElem = $('#form'+curnum).clone().attr('id', 'form' + newNum);
//
//this is where I need to update the onclick to copyform('+newNum+')
$('#form'+newNum+' input').attr('onclick', 'copyform('+newNum+')');
// insert the new element after the last "duplicatable" input field
$('form').after(newElem);
}
我只需要帮助实现
onclick
功能。这是一个更大更复杂的程序的一部分,这将是一个很难展示的例子。非常感谢您的任何帮助 我对你的函数做了一点修改:
函数copyform(curnum){
var curnum=+$(this).closest('[data num]').attr('data-num');
//仅将消息放入当前表单的
if(curnum==3)$('形式'+curnum+'p').html('谢谢!')
//让我们将最后一个表单放入一个变量中进行克隆和追加
var$lastForm=$(“#form”+curnum);
var newNum=1+$('form')。长度;//number+string=>number
var$newElem=$lastForm.clone().attr({
'id':'form'+newNum,
“数据编号”:newNum//将表单编号存储在自定义属性中
});
$lastForm.after($newElem);
}
$(函数(){
//我们不必手动将事件附加到每个克隆表单
// http://api.jquery.com/on/
$('body')。在('click','forminput[type=“button”]”,copyform)上;
});
detailure,你想实现什么?为什么你会使用jQuery而坚持使用onClick属性?使用事件监听器。哦,请不要,为什么要这样绑定单击处理程序?您是否完全没有理解jQuery的要点?
function copyform( curnum ){
var curnum = + $(this).closest('[data-num]').attr('data-num');
// put the message into current form's <p> only
if( curnum == 3) $('#form'+curnum+' p').html('Thank You!')
// let's put the last form into a variable for cloning and appending
var $lastForm = $('#form'+curnum);
var newNum = 1 + $('form').length; // number+string => number
var $newElem = $lastForm.clone().attr({
'id': 'form'+newNum,
'data-num': newNum // store the form number in a custom attribute
});
$lastForm.after( $newElem );
}
$(function(){
// we don't have to attach event manually to each cloned form
// http://api.jquery.com/on/
$('body').on('click','form input[type="button"]', copyform );
});