2016-11-27 50 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

似乎不起作用。 –