2013-03-13 69 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