2013-03-13 72 views
1

我有兩個選擇菜單請求呼叫時,特別選擇選項選擇

  1. 居民的樓號和
  2. 他們的房間號

如果遊客選擇的最後一項功能(兩者都有相同的value,textclass),我想調用一個隱藏表單的函數並顯示一條消息。

工作正常,因爲它是也可以通過一個獨立的鏈接調用的非居民將有可能點擊的功能:(?不要住在這裏)

我不能做的工作是具有selectchange()如果選擇了最後一個選項,則會調用該函數。

這裏是我的選擇代碼的簡要版本:

<form id="document-request" name="document-request" class="clearfix"> 
    <label>Your Full Name: 
     <input type="text" name="fullname" value="" class="span12" 
     required> 
    </label> 
    <label>Your Email: 
     <input type="text" name="email" value="" class="span12 email" 
     required> 
    </label> 
    <label for="building">Your Bldg &amp; Apt#:</label> 
    <select name="building" class="span2" required> 
     <optgroup label="Building"> 
      <option value="" name="building">--</option> 
      <option value="1" name="building">1</option> 
      <option value="2" name="building">2</option> 
      <option value="3" name="building">3</option> 
      <option value="4" name="building">4</option> 
      <option value="5" name="building">5</option> 
      <option value="6" name="building">6</option> 
      <option value="7" name="building">7</option> 
     </optgroup> 
     <option value="nonresident" name="building" class="dontlivehere">I Don’t Live Here</option> 
    </select> 

,這裏是jQuery的:

$('#document-request select').change(function() { 
    var theValue = $(this).child('option:selected').text(); 
    if ((theValue).contains('Live Here')) { 
     residenceAlert(); 
    } 
}); 

我也試圖通過作用於value還有class做到這一點。
我真的不需要那個dontlivehere類,它只是我試圖找到解決方案的一部分。

樣本頁面進行中可以發現here

回答

4

.child()不是一個有效的jQuery的功能,嘗試.find()

var theValue = $(this).find('option:selected').text(); 

我會probabaly只是用這樣的:

if ($(this).find('option:selected').val() === 'nonresident') { 
    residenceAlert(); 
} 
+0

輻接受以前的答案還爲時過早。測試val()(這是我最初開始我的代碼但無法工作的地方)做的竅門。現在只選擇「我不住在這裏」。觸發該功能,一切都很好。現在到下一個問題... 謝謝! – dashard 2013-03-14 01:46:32

0

不應該在該行:

if ((theValue).contains('Live Here')) { 

說:

if ((theValue).contains("I Don't Live Here")) { 
0

這個工作對我來說:

HTML

<select id="building" name="building" class="span2" required> 
    <optgroup label="Building"> 
    <option value="" name="building">--</option> 
    <option value="1" name="building">1</option> 
    <option value="2" name="building">2</option> 
    <option value="3" name="building">3</option> 
    <option value="4" name="building">4</option> 
    <option value="5" name="building">5</option> 
    <option value="6" name="building">6</option> 
    <option value="7" name="building">7</option> 
    </optgroup> 
    <option value="nonresident" name="building" class="dontlivehere">I Don’t Live Here</option> 
    </select> 

的jQuery:

$(document).ready(function(){ 
$('select#building').change(function() { 
     var theValue = $('option:selected').text(); 
     console.log(theValue); 
     if ((theValue).contains('Live Here')) { 
     alert("it works!"); 
    } 
}); 
}); 
+0

你是如何解決'Object has no method'contains''的問題的?你的代碼在[** fiddle **](http://jsfiddle.net/FZ2DD/) – Nope 2013-03-13 22:46:18

+0

嗯,我沒有這個錯誤。看:http://jsfiddle.net/vMAPn/ – 2013-03-13 22:48:08

+0

你連接的小提琴[** http://jsfiddle.net/vMAPn/**](http://jsfiddle.net/vMAPn/)不起作用。它甚至沒有引用jQuery。在Chrome和IE中,默認情況下,大多數情況下請使用瀏覽器開發者控制檯(F12)在使用代碼時檢查錯誤消息。 – Nope 2013-03-13 22:50:45

0

2問題:

1)子方法。

2)包含方法。

var theValue = $(this).find('option:selected').text(); 
if(theValue.indexOf('Dont Live Here') >= 0){ 
    residenceAlert() 
} 
1

瀏覽器調試器控制檯會顯示以下錯誤:

Uncaught TypeError: Object [object Object] has no method 'child' 

child()不是一個jQuery方法。

例如,將其改爲find()

find()更換child()之後,下一個錯誤,你可以得到,這取決於你的瀏覽器是:

Uncaught TypeError: Object has no method 'contains' 

string.contains()只有Firefox支持,使用indexOf()反而可能會更好,除非關閉當然你不」不需要支持任何其他瀏覽器。

你完整的代碼可能類似於:

$('select').change(function() { 
    var theValue = $(this).find('option:selected').text(); 
    if (theValue.indexOf('Live Here')) { 
     residenceAlert(); 
    } 
}); 

DEMO - 使用.find()indexOf()


+0

其實我說得太快了。無論我從菜單中選擇什麼,此代碼都會觸發該功能。 @rusty牛仔褲'的答案做到了。 – dashard 2013-03-14 01:44:28

+0

@dashard:不用擔心。使用您提供的示例HTML並修復現有代碼,代碼按照預期的方式工作,如DEMO中所示。當然,如果這不是你真正需要的,那是一個不同的故事。主要的是你有它排序,很高興你做了:)另外'indexOf'不是jQuery,但標準的JavaScript,並返回一個整數,表示在'theValue'字符串中指定的'Live Here'值被找到的位置。如果沒有發現,則返回-1。 – Nope 2013-03-14 12:03:36