2016-03-07 28 views
1

我的聯繫表單和基於輸入字段的jQuery重定向有點問題。使用Javascript輸入字段值和重定向

一切都很完美,直到我試圖實現重定向以形成驗證碼。

重定向代​​碼:

$("#signup").submit(function(event){ 
    window.location = $('input[type="radio"]:checked').val(); 
}); 

形式:

<form id="signup" name="signup" method="post" novalidate="novalidate"> 

<input type="radio" name="gender" value="http://www.paypal.com" checked> Option 1<br> 
<input type="radio" name="gender" value="http://www.google.com"> Option 2<br> 
<input type="radio" name="gender" value="http://www.seznam.cz"> Option 3<br> 

<input id="submit" type="submit" name="submit" value="Continue to make payment..." class="btn btn-wide btn-extrawide" data-loading-text="Loading..."> 

的jQuery:

// Signup form 
$('#signup').validate({ 
    rules: { 
     name: { 
      required: true, 
      minlength: 2 
     }, 
     email: { 
      required: true, 
      email: true 
     } 
    }, 
    messages: { 
     name: { 
      required: "Please enter your nameee", 
      minlength: "Your name must consist of at least 2 characters" 
     }, 
     email: { 
      required: "Please enter your email address" 
     } 
    }, 
    submitHandler: function(form) { 
     $(form).ajaxSubmit({ 
      type:"POST", 
      data: $(form).serialize(), 
      url:"inc/signup.php", 
      success: function() { 
       $('#signup :input').attr('disabled', 'disabled'); 
       $('#signup').fadeTo("slow", 0.15, function() { 
        $(this).find(':input').attr('disabled', 'disabled'); 
        $(this).find('label').css('cursor','default'); 
        $('#success').fadeIn(); 
       }); 

        // added lines, not working:      
        $("#signup").submit(function(event) { 
         window.location = $('input[type="radio"]:checked').val(); 
        }); 


      }, 
      error: function() { 
       $('#signup').fadeTo("slow", 0.15, function() { 
        $('#error').fadeIn(); 
       }); 
      } 

     }); 
    } 
}); 

將竭誠爲您能給我任何反饋。萬分感謝。

+0

亞當,這裏是什麼問題?請指定問題陳述 – ChiranjeeviIT

+1

使用'window.location.href'而不是'window.location' – Jorrex

+0

是的,使用'window.location.href'。 –

回答

0

您與您添加的行再次提交表單:

// added lines, not working:      
$("#signup").submit(function(event) { 
    window.location = $('input[type="radio"]:checked').val(); 
}); 

所以阿賈克斯後提交它什麼也不做,因爲當你做$("#signup").submit它沒有做任何的提交按鈕被禁用由於此代碼:

$('#signup :input').attr('disabled', 'disabled');

因此,要解決這個問題,從這個上面修改添加的代碼:

$("#signup").submit(function(event) { 
    window.location = $('input[type="radio"]:checked').val(); 
}); 

要這樣:

window.location.href = $('input[type="radio"]:checked').val(); 
+0

工作就像一個魅力!謝謝羅賓。 – Adam

+0

@亞當太棒了!很高興幫助。 –