【问题标题】:.val() JQuery. Passing text fields to jQuery.val() jQuery。将文本字段传递给 jQuery
【发布时间】:2015-05-02 20:05:28
【问题描述】:

基本上我的代码非常好,只是当我创建它们以使用 api 搜索功能时,我似乎无法获得额外添加的标签。我想将它们传递到标签字段以及 $("#textbox1").val()。任何提示/帮助表示赞赏。谢谢

<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(){

	    var counter = 2;
		
	    $("#addButton").click(function () {
				
			 
			
			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++;
	    });

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

    <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>

【问题讨论】:

  • 很高兴看到这个问题。我做了很多工作,然后在你删除之前 12 秒发布了它
  • 谢谢!,没见过,哈哈。很高兴看到反馈!

标签: javascript jquery html json textfield


【解决方案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>

【讨论】:

    猜你喜欢
    • 2013-07-13
    • 1970-01-01
    • 1970-01-01
    • 2016-03-31
    • 2014-05-26
    • 1970-01-01
    • 2012-08-19
    • 2013-08-22
    • 2013-07-28
    相关资源
    最近更新 更多