如何在提交表单时传递javascript值?

时间:2014-03-10 10:54:43

标签: javascript asp.net-mvc-4 asp.net-mvc-routing

我是asp.net mvc4的新手。我想在提交表单时传递java脚本值。我想传递javascript中随机字段的值以及文本框值。请帮我。提前致谢。 这是我用于生成和验证验证码的Javascript代码:

<script type="text/javascript">
    var captchastring = '';
    function getCaptcha() {
        var chars = "0Aa1Bb2Cc3Dd4Ee5Ff6Gg7Hh8Ii9Jj0Kk1Ll2Mm3Nn4Oo5Pp6Qq7Rr8Ss9Tt0Uu1Vv2Ww3Xx4Yy5Zz";
        var string_length = 7;
        captchastring = '';
        for (var i = 0; i < string_length; i++) {
            var rnum = Math.floor(Math.random() * chars.length);
            captchastring += chars.substring(rnum, rnum + 1);
        }
        document.getElementById("randomfield").innerHTML = captchastring;
        document.getElementById("forgot_password_form").submit();
    }

    function validation() {
        var text2 = document.getElementById("txtcode").value;
        if (text2 == captchastring) {

            var email = document.getElementById("UserEmail").value;

            x = document.getElementById("demo");
            // Find the element

            x.innerHTML = "valid";
            x.style.color = "#ff0000";

        }
        else {

            x = document.getElementById("demo");  // Find the element
            x.innerHTML = "Invalid Captcha. Try again";
            x.style.color = "#ff0000";
            return false;
        }
    }

 </script>

提交表格的代码:

    @using (Html.BeginForm("Forgotpassword", "Login", FormMethod.Post, new { @class="forgot_password_form"})) 
    {        
@Html.TextBoxFor(u => u.UserEmail, new { @class = "form-control", placeholder = "Email" })
<input type="text" name="txtcaptcha" id="txtcaptchaid"  class="form-control" placeholder="Enter code here" />
    <input type="Submit" class="btn btn-success btn-block" style="float: right; width:30%;" value="Reset Password" onclick="validation()" /> <p id="demo"> </p>  
    }

0 个答案:

没有答案