无法获取电子邮件地址onclick功能

时间:2016-11-10 08:23:42

标签: javascript jquery html

<div id="divForm" class="fancybox" style="display:none;">
    <form id="frm_step1" action="download1.php" method="post">
        <label>Enter Email</label>
        <input type="email" name="email" id="email" value="" required />
        <input type="button" name="submit" id="submit" value="Submit" class="form-submit" target-form="frm_step1" onclick="test();" />
    </form>
</div>
function test() {
    var email = $('#email').text();
    alert(email);
    $.ajax({
        type: "POST",
        dataType: "json",
        url: 'download1.php?email' + email,
        data: { 'email': email },
        success: function(data) {
            window.location.href = 'download.php#file';
        },
        error: function(e) {
            alert('Error: ' + e);
        }
    });
}

我无法收到此邮件。当我发出警报时,它返回空白。 而且我总是像这样使用jQuery / AJAX,但这次它不会提醒电子邮件地址。

1 个答案:

答案 0 :(得分:2)

使用.val()代替.text().val()方法主要用于获取表单元素的值,例如输入选择 textarea

$('#email').val();

function test() {
    var email = $('#email').val();
    alert(email);
    $.ajax({
        type: "POST",
        dataType: "json",
        url: 'download1.php?email' + email,
        data: { 'email': email },
        success: function(data) {
            window.location.href = 'download.php#file';
        },
        error: function(e) {
            alert('Error: ' + e);
        }
    });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="divForm" class="fancybox">
    <form id="frm_step1" action="download1.php" method="post">
        <label>Enter Email</label>
        <input type="email" name="email" id="email" value="" required />
        <input type="button" name="submit" id="submit" value="Submit" class="form-submit" target-form="frm_step1" onclick="test();" />
    </form>
</div>