如何从一个div获取值到另一个onclick

时间:2016-03-21 17:33:58

标签: javascript jquery

使用搜索功能,当用户点击按钮应用搜索时,我希望该值保留在字段中。

HTML

for var i = 1; i <= 100; ++i {
if i / 3 {
    print("Hey")
}

else if i / 5 {
    print("Hello")
}

else if i / 3 && i / 5 {
    print("Great")
}

else {
    print(i)
}
}

或jquery

 <div class="search">
       <input type="text" id="searchtext" placeholder="Search">
       <a href="#" class="search-text button">GO</a>
 </div>

 $('.search button').on('click',function(e){
      e.preventDefault();
     document.getElementById("searchtext").innerHTML = document.getElementById("searchtext").innerHTML;
 });

也尝试了这个:

 $('#searchtext').val() = $('#searchtext').val();

当用户点击搜索按钮时,如果他们搜索“文本”,则“文本将保留在页面加载的字段中,删除任何占位符。

3 个答案:

答案 0 :(得分:0)

忘了你似乎用它自己的innerHTML值(没有任何东西)填充一个DIV的innerHTML,你混合了jQuery和vanilla JS。你应该做一个或另一个:

jQuery的:

$('.search button').on('click',function(e){
  e.preventDefault();
  var searchElement = $("#searchtext");
  searchElement.html(searchElement.html());
});

Vanilla JS:

document.querySelectorAll('.search button')[0].addEventListener('click',  function(e) {
  e.preventDefault();
  var searchElement = document.getElementById('searchtext');
  searchElement.innerHTML = 'test';
});

答案 1 :(得分:0)

使用 jQuery

    $('.search button').on('click',function(e){
      e.preventDefault();
      var val = $("#searchElement").html();
      $("#searchElement").html(val );
    });

答案 2 :(得分:0)

我在这个方面有一些帮助,但基本上你存储了查询然后将其拆分,以便将它放入你需要的值的div中。

class Product < ActiveRecord::Base
searchkick settings: {
          number_of_shards: 5,
          analysis: {
            filter: {
              hunspell_CZ: {
                type: 'hunspell',
                locale: 'cs_CZ',
                dedup: true,
                recursion_level: 0
              },
              czechStemmer: {
                type: 'stemmer',
                name: 'czech'
              },
            #  customWordDelimiter: {
            #    type: 'word_delimiter',
            #    generate_word_parts: true,
            #    generate_number_parts: true,
            #    catenate_words: true,
            #    catenate_numbers: true,
            #    catenate_all: true,
            #    split_on_case_change: true,
            #    preserve_original: true,
            #    split_on_numerics: true,
            #    stem_english_possessive: false
            #  },
              customUnique: {
                type: 'unique',
                only_on_same_position: false
              }
            },
            analyzer: {
              myFulltextAnalyzer: {
                type: 'custom',
                tokenizer: 'standard',
                filter: ['lowercase', 'czechStemmer', 'customUnique', 'icu_folding', 'hunspell_CZ'],#'customWordDelimiter',
                char_filter: ['html_strip']
              }
            }
          }
      },
      text_middle: [ :cs_product, :ma_product, :en_product, :cs_description, :en_description, :ma_description ], 
      index_name: "products_v1", 
      merge_mappings: true,
      mappings: {
        product: {
          properties: {
            cs_product: {type: "string", analyzer: "myFulltextAnalyzer", index: "analyzed"},
            en_product: {type: "string", analyzer: "myFulltextAnalyzer", index: "analyzed"},
            ma_product: {type: "string", analyzer: "myFulltextAnalyzer", index: "analyzed"},
            cs_description: {type: "string", analyzer: "myFulltextAnalyzer", index: "analyzed"},
            en_description: {type: "string", analyzer: "myFulltextAnalyzer", index: "analyzed"},
            ma_description: {type: "string", analyzer: "myFulltextAnalyzer", index: "analyzed"}
          }
        }
      }