0

嗨,我很久以前就遇到过这个问题,我浪费了很多时间来找出问题所在。让我讲一下情节。假设我有一个 html 页面,其中我已经在页面中的元素很少,并且我正在使用 ajax 调用动态添加一些元素。在向页面添加新项目时,我遇到了这样的情况,例如我需要为其中一个元素添加 onClick 函数调用以及作为参数的对象。但是当我在 jquery 中添加该函数时,函数被调用但我在函数中得到的对象与我传​​入的对象不同。对象被转换为字符串而不是对象。

<html>
<body>
.
.
.
... Some html code....
.
.
.
.
<div onclick="function1();"> </div>
.
.
<div id="dynamicElement"> </div>
.
.
</body>
<script type="text/javascript">
function function1(){
   .
   .
   $.ajax(){

    success(obj):{ 
          $("#dynamicElement").empty().append('<input id="iButton" type="button" onclick="function2('+obj+')">')
        }
   }
}
</script>

<script type="text/javascript">
 function2(obj){
   //Here When I try to manipulate with this object. I was actually not abt to do.
   // this is because of the object u add in dynamic elements will get converted to string instead of ibject.
 }
</script>
</html>
4

1 回答 1

0

这个问题的解决方案是。我们甚至需要在外部添加 onclick,而不是在添加元素本身时指定它。

上述示例的解决方案:在 Ajax 调用中只需添加按钮元素。

$.ajax(){

    success(obj):{ 
          $("#dynamicElement").empty().append('<input id="iButton" type="button">');
      $("#iButton").onClick({
          function2(obj);
         });
        }
   }

现在它完美地工作了。

于 2013-09-25T12:46:17.717 回答