2016-11-27 24 views
-4

我对此很陌生,无法弄清楚我出错的地方。当我点击按钮时,什么都没有发生。我可能会增加太多额外的东西,我无法确定。任何帮助是极大的赞赏。我在这段代码中遗漏了什么?

<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript" /> 
    <html ng-app> 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> 
<script src="victory.js"></script> 
<script language="javascript" type="text/javascript"> 
var Walmart = (function() { 
function Walmart(http, zone) { 
    this.stores = []; 
    this.storeid = document.getElementById("storenum").value 
    this.walmart = { 
      query: [ 
       'http://search.mobile.walmart.com/search?query=', 
       '&store=' + this.storeid 
      ], 
      }; 
    this.zone = zone; 
    this.http = http; 
    console.log(this.walmart); 

    }; 

var query = document.getElementById("search").value; 

Walmart.prototype.getWalmartStoreQuery = function (query) { 
    return this.http.get(this.walmart.store.query[0] + query + this.walmart.store.query[1]).map(function (res) { return res.json(); }); 

</script> 
<tr> 
<td><input type="text" name="storenum" id="storenum"></td> 
<td><input type="text" name="search" id="search"></td> 
</tr> 
<tr> 
<td><button onclick="getWalmartStoreQuery">Test</button></td> 
</tr> 
+0

检查控制台是否有错误信息? –

+0

自带作为一个错误的唯一的事情是“GET /favicon.ico”错误(404):“未找到” – MrKane123

+0

所以没有错误消息,当您单击该按钮? –

回答

-3

不能从按钮的onclick事件类实例化一个函数

+0

那么我会怎么做呢? – MrKane123

+0

您还必须通过“http”和“区域”: –

+0

walmart(http,zone).getWalmartStoreQuery() –

1

如果你打算要连接一个事件处理程序通过HTML属性的HTML元素,代码应该是:

onclick="getWalmartStoreQuery()"不,onclick="getWalmartStoreQuery"

但是,这是不是真的推荐的方法。事件布线的这种形式被称为“内联事件布线”和:

  1. 创建面条代码不促进关注良好的分离,使调试更难。

  2. 在您的代码周围创建一个隐藏的全局匿名代理函数,该函数操纵绑定到Global的this

  3. 不遵循W3C DOM Level 2 Event Model,不一样强大这一模式规定。

要连接一个事件处理回调的更现代的方式,一旦DOM被加载在你的JavaScript做到这一点:

var btn = document.getElementById("theButtonsId"); 

// Do not put parenthesis after the callback name as 
// you are only trying to reference it here, not call it 
btn.addEventListener("click", callBackFunctionName); 
-1

测试此:

var walmart = function(http, zone){ 
 
    \t \t if(!(this instanceof walmart)){ 
 
    \t \t \t return new walmart(http, zone); 
 
    \t \t } 
 
    
 
    \t this.stores = []; 
 
     this.storeid = document.getElementById("storenum").value 
 
     this.walmart = { 
 
      query: [ 
 
        'http://search.mobile.walmart.com/search?query=', 
 
        '&store=' + this.storeid 
 
       ], 
 
      }; 
 
     this.zone = zone; 
 
     this.http = http; 
 
    }; 
 
    
 
    walmart.prototype.getWalmartStoreQuery = function() { 
 
    \t var query = document.getElementById("search").value; 
 
     return this.http.get(this.walmart.store.query[0] + query + this.walmart.store.query[1]).map(function (res) { 
 
     \t return res.json(); 
 
     }); 
 
    }; 
 
<tr> 
 
    <td><input type="text" name="storenum" id="storenum"></td> 
 
    <td><input type="text" name="search" id="search"></td> 
 
    </tr> 
 
    <tr> 
 
    <td><button onclick="walmart.prototype.getWalmartStoreQuery()">Test</button></td> 
 
    </tr>

+0

似乎不起作用。 –