[英]How will I change the id attribute according the condition?
In the below snippet I'm changing the id attribute value of the UPDATE
button. 在下面的代码段中,我正在更改
UPDATE
按钮的id属性值。 But while its id value is changed then according its change id why jQuery is not working well according update
button id
attribute. 但是,虽然更改了其id值,然后根据其更改id却根据
update
按钮id
属性为何jQuery无法正常工作。
$('#save').click(function(){ console.log(this.value); if(this.value == 1){ $("#update").attr('id','update1'); } else{ $("#update").attr('id','update2'); } console.log($('.update').attr("id")); }); $('#update').click(function(){ console.log($('.update').attr("id")); console.log("hello update"); }); $('#update1').click(function(){ console.log($('.update').attr("id")); console.log("hello update1"); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input class="textfield" type="text" placeholder="Enter"> <button id ="save" value= "1">save</button> <button id ="update" class="update">Update</button>
How will I do it that When save button is clicked then id
value is changed according its condition. 当单击保存按钮然后
id
值根据其条件更改时,我该怎么做。 And according the value of the id
attribute of the update button how will Jquery works means if id="update1"
then in console.log
it will print "hello update1" else "update". 并根据更新按钮的
id
属性的值,Jquery的工作方式意味着,如果id="update1"
则在console.log
它将打印“ hello update1”,否则显示“ update”。 Any help will be appreciated. 任何帮助将不胜感激。 Thank you.
谢谢。
It seems the since the event is already attached to the DOM element, updating id and clicking on it is making no difference. 似乎因为该事件已经附加到DOM元素上,所以更新id并单击它没有任何作用。
You can try by delegating the event using on
您可以通过使用委派事件尝试
on
$('#save').click(function() { console.log(this.value); if (this.value == 1) { $("#update").attr('id', 'update1'); } else { $("#update").attr('id', 'update2'); } console.log($('.update').attr("id")); }); $('#update').click(function() { console.log($('.update').attr("id")); console.log("hello update"); }); $('body').on('click', '#update1', function() { // changed here console.log($('.update').attr("id")); console.log("hello update1"); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input class="textfield" type="text" placeholder="Enter"> <button id="save" value="1">save</button> <button id="update" class="update">Update</button>
@vikas kumar, try with below solution, @vikas kumar,请尝试以下解决方案,
$(document).ready(function(){ $('#save').click(function(){ if(this.value == 1){ $("#update").attr('id','update1'); } else{ $("#update").attr('id','update2'); } }); $('.update').click(function(){ console.log($('.update').attr("id")); switch($('.update').attr("id")) { case "update": console.log("hello update"); break; case "update1": console.log("hello update1"); break; case "update2": console.log("hello update2"); break; } }); })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input class="textfield" type="text" placeholder="Enter"> <button id ="save" value= "1">save</button> <button id ="update" class="update">Update</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.