两个不同的选择使用相同的选项,然后设置溢出div或错误消息的动画

时间:2012-09-29 17:52:16

标签: jquery

编辑:哦,好吧,现在我正在设法让它正常工作。但是如果用户在收到错误消息后选择了正确的选项,我需要将错误消息发送到.hide。

这是html / jquery im正在进行

 <form>
 <select>
     <option>Please Select Your Value</option>
     <option>Pick this value 1</option>
 </select>
 <div class="error">Please select your value</div>

 <select>
     <option>Please Select Your Value</option>
     <option>Pick this value 2</option>
 </select>
     <div class="error">Please select your value</div>

     <input type="submit" value="Show Hidden Div" />         
 </form>

 <div class="suprise">
   Hidden Div
 </div>

和jQuery

$('.submit').click(function(){
   var correct = true;
   $('select').each(function(){
     if($(this).val() != 'option2value'){
       //next is the error... it might be better to use an id there
       $(this).next().show();
       correct = false;
     }
   })
   if(correct){
    $(".results").animate({ 
            marginTop: -480}, 400); 
   }
 })

我只需要一点帮助,如何做出正确的触发......我不知道该怎么做。

谢谢你们!

2 个答案:

答案 0 :(得分:0)

首先,您需要在select

中插入一个ID
<select id="select1">
     <option>Please Select Your Value</option>
     <option>Pick this value 1</option>
 </select>
 <div class="error">Please select your value</div>

 <select id="select2">
     <option>Please Select Your Value</option>
     <option>Pick this value 2</option>
 </select>

$("select[id^=select]").on("change", function() {
    if ($(this).attr("id") == "select1") {
        if ($(this).val() == $("#select2").val()) {
            alert("You are wrong");
        } else {
            alert("You are right");
        }
    }
    else {
        if ($(this).val() == $("#select1").val()) {
            alert("You are wrong");
        } else {
            alert("You are right");
        }
    }
})​

答案 1 :(得分:0)

伙计们,对不起。我在浏览.next时遇到了麻烦而没有设置.error div ...现在正在工作,谢谢你的帮助。