简体   繁体   English

Onkeyup在其他功能中不起作用

[英]Onkeyup doesn't work in another function

add image dynamically, but I have a problem when I input link in textbox images are not showing. 动态添加图像,但是当我在文本框中输入的链接未显示时出现问题。

Here's a screenshot : 这是屏幕截图: 在此处输入图片说明

In FireBug I see the following error: 在FireBug中,我看到以下错误:

ReferenceError: toggle is not defined ReferenceError:未定义切换

Here is my code: 这是我的代码:

<script>
// Max Image
var maxSlide = 2000;
// Start Image
var curSlide = 0;
// Start Id dynamic
var Ids = 0;

function generateSlide() {
  // If Textboxt input link image, this id="img_dynamicid" src show
  function toggle(element) {
    we = element.value;
    document.getElementById('img_'+Ids+'').src=we;  
  } 

  if( curSlide < maxSlide ) {
    var html ='<div class="row" id="slideAdd_'+Ids+'" >';
    html+='     <div class="col-md-12">';
    html+='         <div class="box">';
    html+='             <div class="box-content">';
    html+='                 <form action="#" id="slide_'+Ids+'" class="form-horizontal" enctype="multipart/form-data">';
    html+='                     <div class="form-group">';
    html+='                         <div class="col-sm-9 col-lg-10 controls">';
      html+='<center><img src="../images/noimage.png" id="img_'+Ids+'" style="width:150px;height:150px"></center><br/>';
    html+='                         </div>';
    html+='                     </div>';
    html+='                     <div class="form-group">';
    html+='                         <label class="col-sm-3 col-lg-2 control-label">Link Image</label>';
    html+='                         <div class="col-sm-9 col-lg-10 controls">';
    html+='                             <input type="text" onkeyup="toggle(this)" placeholder="Link Image" name="link_'+Ids+'" id="judul_'+Ids+'" class="form-control" required>';
    html+='                         </div>';
    html+='                     </div>';
    html+='                     <div class="form-group">';
    html+='                         <div class="col-sm-9 col-sm-offset-3 col-lg-10 col-lg-offset-2">';
    html+='                             <button onClick="removeSlide(\''+Ids+'\'); return false;" class="btn btn-danger"><i class="fa fa-times"></i> Remove</button>';
    html+='                         </div>';
    html+='                     </div>';
    html+='                 </form>';
    html+='             </div>';
    html+='         </div>';
    html+='     </div>';
    html+=' </div>';
    $("#main-content").append(html);
    curSlide++;
    Ids++;
    $("#counter").html(curSlide+" from 2000");
  }
}
</script>

<button type="submit" class="btn btn-primary" onClick="generateSlide()" ><i class="fa fa-plus"></i>Add Images</button>
<span id="counter" >0 from 2000</span>

To be honest, you should attach events using jQuery. 老实说,您应该使用jQuery附加事件。 But if you want to do this way, you should put the toggle() outsite and pass the id parameter to it 但是,如果要这样做,则应将toggle()放到外部,然后将id参数传递给它

function toggle(id){
    document.getElementById('img_'+id+'').src = this.value;
  } 


function generateSlide() {

// ...
     html+='<input type="text" onkeyup="toggle(' + Ids + ')" placeholder="Link Image" name="link_'+Ids+'" **id="judul_'+Ids+'"** class="form-control" required>';
// ...
}

You are defining the toggle function inside your function and thus it isn't accessible in global scope where the event handler is tried to be executed. 您在函数内部定义了切换功能,因此无法在试图执行事件处理程序的全局范围内访问它。 Thus you need to move the toggle definition out of the scope of function generateSlide . 因此,您需要将切换定义移出函数generateSlide的范围。

It should look like 它看起来像

function toggle(element) {
  we = element.value;
  document.getElementById('img_'+Ids+'').src=we;  
} 
function generateSlide() {
  // If Textboxt input link image, this id="img_dynamicid" src show

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM