2014-09-11 113 views
-1

我有4個按鈕。無法正常工作移動到另一個頁面

其中三個顯示許可證。 其中一個標題爲「企業」必須轉到另一個頁面。

不幸的是,當我點擊'企業'時刻顯示許可證,然後頁面轉到另一頁。

我該如何讓代碼不傳遞顯示許可證,而是立即轉到另一頁?

我的代碼:

$('input[title="enterprise"]').click(function(){ 
    window.location.href = "/contact"; 
}); 

$('input[type=button]').click(showLicence).each(function() { 
    this.version = this.title; 
    this.title = "Buy "; 
    switch(this.version) { 
     case 'basic': 
      this.title += 'Basic'; 
      break; 
     case 'standard': 
      this.title += 'Standard'; 
      break; 
     case 'business': 
      this.title += 'Business'; 
      break; 
    } 
}); 
+3

我不是在JavaScript親,但不是這個選擇器實際上選擇所有的按鈕:input [type = button]? – Hristo 2014-09-11 12:31:23

回答

5

嘗試使用:not

$('input[type=button]:not([title="enterprise"])').click(showLicence).each(function() { 
5

更改你的第二個事件處理程序,以便enterprise按鈕被排除:

$('input[type=button]:not([title="enterprise"])').click(showLicence).each(function() { 
4

添加一類獨特的分離這兩個功能。

對於如:

<button class="enterprise">Enterprise</button> 
<button class="normal">Basic</button> 
<button class="normal">Standard</button> 
<button class="normal">Business</button> 

然後在JS,做了細微的變化:

$('.enterprise').click(function(){ 
    window.location.href = "/contact"; 
}); 

$('.normal').click(showLicence).each(function() { 
    this.version = this.title; 
    this.title = "Buy "; 
    switch(this.version) { 
     case 'basic': 
      this.title += 'Basic'; 
      break; 
     case 'standard': 
      this.title += 'Standard'; 
      break; 
     case 'business': 
      this.title += 'Business'; 
      break; 
    } 
}); 

您可以根據您想要的所有屬性添加到該按鈕。希望這是有道理的。

2

也許你可以使用 '默認' 開關關鍵字(或添加一個 '企業' 的情況下):

$('input[type=button]').click(showLicence).each(function() { 
this.version = this.title; 
this.title = "Buy "; 
switch(this.version) { 
    case 'basic': 
     this.title += 'Basic'; 
     break; 
    case 'standard': 
     this.title += 'Standard'; 
     break; 
    case 'business': 
     this.title += 'Business'; 
     break; 
    default: 
     window.location.href = "/contact"; 
} 

});