簡體   English   中英

將輸入的文本字段值傳遞給getJSON字符串?

[英]Passing entered text field value into getJSON string?

我正在嘗試使用flickr API向此圖像搜索器中輸入的標簽添加一些功能。 我有它的工作,但僅當我在代碼中編寫字符串時,它才有效。 例如,我現在有“貓”。 我的目標是將輸入的值從文本框(id =“ textbox1”)傳遞到上面腳本中的標簽中。 我希望能夠從文本框中搜索而不是進入代碼。 感謝您的任何提示/建議!

 <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: "cats", tagmode: "any", format: "json" }, function(data) { console.log(data); $.each(data.items, function(i, item) { $(".buttonclick").remove(); $('<img/>').attr("src", item.media.m).appendTo('#images'); if (i == 1) return false; }); }); }); $('#images').on('click', 'img', function() { var $imgClone = $(this).clone().attr('src', $(this).attr('src').replace('_m.', '_b.')); $('#main').html($imgClone); }); }); $(document).ready(function() { var counter = 2; $("#addButton").click(function() { if (counter > 10) { alert("Only 10 textboxes allow"); return false; } var newTextBoxDiv = $(document.createElement('div')).attr("id", 'TextBoxDiv' + counter); newTextBoxDiv.after().html('<label></label>' + '<input type="text" name="textbox' + counter + '" id="textbox' + counter + '" value="" >'); newTextBoxDiv.appendTo("#TextBoxesGroup"); counter++; }); $("#removeButton").click(function() { if (counter == 1) { alert("No more textbox to remove"); return false; } counter--; $("#TextBoxDiv" + counter).remove(); }); $("#getButtonValue").click(function() { var msg = ''; for (i = 1; i < counter; i++) { msg += "\\n Textbox #" + i + " : " + $('#textbox' + i).val(); } alert(msg); }); }); </script> <h1>Image Search</h1> <button type="button" id="button">Search</button> <input type='button' value='Remove Tag' id='removeButton'> <div id='TextBoxesGroup'> <div id="TextBoxDiv1"> <input type='textbox' id='textbox1'> <input type='button' value='Add tag' id='addButton'> </div> </div> <div id="images" /></div> </body> </html> 

$ .getJSON就可以切換“ ”與

$("#textbox1").val(),

並且它將使用輸入的值(無論用戶鍵入了什么)作為“標簽”的值。

//Stuff before

tags:$("#textbox1").val(),

//Stuff after

希望對您有所幫助!

編輯:如果您想了解有關jQuery的.val()的更多信息,可以在此處閱讀文檔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM