2015-05-02 28 views
-1

基本上我的代碼非常好,它只是在我創建它們以使用api搜索具有任何功能時似乎無法獲得額外添加的標籤。我想將它們傳遞到標籤字段以及$(「#textbox1」)。val()。任何提示/幫助表示讚賞。由於.val()JQuery。將文本字段傳遞給jQuery

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<html> 
 
<head> 
 
    <script type="text/javascript" src="jquery-1.11.2.js"></script> 
 
</head> 
 
    
 
<body> 
 
    <script type="text/javascript"> 
 
    
 
     
 
    $(document).ready(function(){ 
 
    $("#button").click(function(){ 
 
     $("#images").empty(); 
 

 

 

 
     $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?", 
 
      { 
 
       tags:$("#textbox1").val(), 
 
       tagmode: "any", 
 
       format: "json" 
 
      }, function(data){ 
 
       console.log(data); 
 
       $.each(data.items, function(i,item){ 
 
        
 

 

 
        $('<img/>').attr("src", item.media.m).appendTo('#images'); 
 
        if(i==2) return false; 
 
       }); 
 
     }); 
 
    
 
    }); 
 
    $('#images').on('click', 'img', function(){ 
 
     
 
    }); 
 
}); 
 

 

 
    $(document).ready(function(){ 
 

 
\t  var counter = 2; 
 
\t \t 
 
\t  $("#addButton").click(function() { 
 
\t \t \t \t 
 
\t \t \t 
 
\t \t \t 
 
\t \t \t var newTextBoxDiv = $(document.createElement('div')).attr("id", 'TextBoxDiv' + counter); 
 
       newTextBoxDiv.after().html('<label></label>' + 
 
\t \t \t \t '<input type="text" name="textbox' + counter + 
 
\t \t \t \t '" id="textbox' + counter + '" value="" >'); 
 
      
 
\t \t \t newTextBoxDiv.appendTo("#TextBoxesGroup"); 
 
\t \t \t \t 
 
\t \t  counter++; 
 
\t  }); 
 

 
\t \t 
 
\t \t $("#getButtonValue").click(function() { 
 
\t \t 
 
\t \t \t var msg = ''; 
 
\t \t \t for(i=1; i<counter; i++){ 
 
\t \t \t \t msg += "\n Textbox #" + i + " : " + $('#textbox' + i).val(); 
 
\t \t \t } 
 
\t \t  \t alert(msg); 
 
\t \t }); 
 
\t \t 
 
    }); 
 
     
 
     
 
</script>  
 
<button type="button" id="button">Find image</button> 
 

 
    <div id='TextBoxesGroup'> 
 
\t <div id="TextBoxDiv1"> 
 
     <input type='textbox' id='textbox1' > 
 
     <input type='button' value='Add tag' id='addButton'/> 
 
\t </div> 
 
</div> 
 

 

 
    <div id="images" /> </div> 
 

 
    
 
    
 
</body> 
 
</html>

+0

很高興再次看到這個問題。我工作了一段時間,然後發佈它12秒鐘之前,你刪除它 – mplungjan

+1

謝謝!,從來沒有見過它大聲笑。很高興看到反饋! – herenowthere12

回答

1
  1. 請格式化你的代碼
  2. 請只有一個準備
  3. 使用類

$(document).ready(function() { 
 
    $("#button").click(function() { 
 
     $("#images").empty(); 
 
     var tags = []; 
 
     $(".textbox").each(function() { 
 
     tags.push(this.value); 
 
     }); 
 
     console.log(tags) 
 
     $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?", { 
 
     tags: tags.join(" "), 
 
     tagmode: "any", 
 
     format: "json" 
 
     }, function(data) { 
 
     console.log(data); 
 
     $.each(data.items, function(i, item) { 
 
      $('<img/>').attr("src", item.media.m).appendTo('#images'); 
 
      if (i == 2) return false; 
 
     }); 
 
     }); 
 
    }); 
 
    $('#images').on('click', 'img', function() { 
 
    }); 
 

 
    var counter = 2; 
 

 
    $("#addButton").click(function() { 
 
     var newTextBoxDiv = $(document.createElement('div')).attr("id", 'TextBoxDiv' + counter); 
 
     newTextBoxDiv.after().html('<label></label>' + 
 
     '<input type="text" class="textbox" name="textbox' + counter + 
 
     '" id="textbox' + counter + '" value="" >'); 
 
     newTextBoxDiv.appendTo("#TextBoxesGroup"); 
 
     counter++; 
 
    }); 
 
    $("#getButtonValue").click(function() { 
 
     var msg = ''; 
 
     for (i = 1; i < counter; i++) { 
 
     msg += "\n Textbox #" + i + " : " + $('#textbox' + i).val(); 
 
     } 
 
     alert(msg); 
 
    }); 
 
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 

 
<button type="button" id="button">Find image</button> 
 

 
<div id='TextBoxesGroup'> 
 
    <div id="TextBoxDiv1"> 
 
    <input type='textbox' class="textbox" id='textbox1'> 
 
    <input type='button' value='Add tag' id='addButton' /> 
 
    </div> 
 
</div> 
 

 
<div id="images" /></div>