单击按钮,除了两个元素的值和显示结果

时间:2017-07-17 10:53:14

标签: javascript html phonegap

我总共有3个元素

  1. 文本框
  2. 下拉
  3. 按钮
  4. 我想要的是......按钮点击它应该从TextBox& DropDown并返回Button Click

    上的数据

    this.html

        <div class="input" div id="Searching"><input type="text" name="Search" id="Search" class="ContextualSearchField" placeholder="Search" size="40" value=""/></div>
        &nbsp;&nbsp;
         <div class="dropdown">
          <button class="dropbtn">Search&#x25BC;</button>
          <div id="myDropdown" class="dropdown-content">
            <a id="Alert" type="button" class="btn btn-lg btn-default" href="#">Alert</a>
            <a id="IR" type="button" class="btn btn-lg btn-default" href="#">Incident Report</a>
            <a id="Site" type="button" class="btn btn-lg btn-default" href="#">Site</a>
            <a id="Devices" type="button" class="btn btn-lg btn-default" href="#">Devices</a>
          </div>
        </div> 
        <button type="submit" onclick="SearchContext()" id="searchbutton">Search Now</button>
    

    this.js

    var Search = document.getElementById("Search").value;
    alert(Search);
    Dropdown = document.getElementById("myDropdown").value;
    SearchButton = document.getElementById("searchbutton").value;
    var Alert = document.getElementById("Alert").value;
    var IR = document.getElementById("IR").value;
    var Site = document.getElementById("Site").value;
    var Devices = document.getElementById("Devices").value;
    

    我还在jquery.services.js

    中为他们创建了服务

    告诉我如何将这些参数放在按钮

1 个答案:

答案 0 :(得分:1)

只需在按钮上放置一个事件监听器,而不是放在此范围内的事物。

document.querySelector('.dropbtn').addEventListener('click',function(){
    //do your required things in here. E.g:
    var Site = document.getElementById("Site").value;
    var Devices = document.getElementById("Devices").value;
});

如果要将它们放在元素中,只需将参数添加到元素即可。 E.g:
<button id="example" lang="tr" user-id="15" target="Search">Click me</button>和从JS获取此参数,使用HTML元素函数getAttribute()执行此操作:

var lang    = document.getElementById('example').getAttribute('lang');
var user_id = document.getElementById('example').getAttribute('user-id');