2016-11-04 93 views
0

嗨,我只是想用一个按钮从文本字段获取值。我有这样的:通过按钮获得价值

// Add extra contact clicked? 
$('#add-contact p a').click(function(e) { 
    console.log('hallo'); 
    var emailValue = $("input#contactpersonen-email").val(); 
    console.log(emailValue); 

    // Make a copy of the first input fields 
    html = $('#new-contact').children().clone(); 
    //$("#contactpersonen-email").val($("#contactpersonen-email1").val()); 


    // Get number of tabs in the accordion 
    var index = $('#accordion h3').length; 

    // Remove the values 
    html.find("input[type=text]").val(""); 
    html.find('input[type=checkbox]').attr('checked', false); 

    // New 'id', 'for' and 'name' attribute names 
    html.find('input[type=checkbox]').each(function() { 


    me = $(this); 
    attr = me.attr('id'); 
    number = attr.split('_')[2]; 
    newNumber = parseInt(index) + 1; 
    newAttr = attr.replace(number, newNumber); 
    me.attr('id', newAttr).attr('name', newAttr).next().attr('for', newAttr); 

    }); 

    // Insert it at the end 

    $('#accordion').append(html); 
    $('#accordion').accordion('refresh'); 

    // Set last tab to active 
    $("#accordion").accordion({ 
    active: index 
    }); 

    // Cancel the click 
    return false; 
}); 

所以我这样做只是这一点:

var emailValue = $("input#contactpersonen-email").val(); 
            console.log(emailValue); 

,这是HTML:

<div class="contact-label span2"> 
    <label for="contactpersonen-email">Email adres</label> 
    <div class="contact-input-field"> 
    <input type="text" class="input-text span2 required contactpersonen_email" id="contactpersonen_email" name="contactpersonen_email"></input> 
    </div> 
</div> 

,但每次我不确定回来的时间?那么如何获得价值呢?我不明白

谢谢

+0

您可以使用您的选择'contactpersonen-email'但ID为'contactpersonen_email' – Turnip

+0

哦,我没看到你的评论。你应该发布你的答案。 –

回答

1

看起来你有一个错字。这应该工作:

$(function() { 
    var emailValue = $("#contactpersonen_email").val(); 
    console.log(emailValue); 
}); 
+0

谢谢。但我点击功能已经在功能点击功能是在$(文件)。就绪(函数() \t \t \t \t \t \t \t { – SavantCode

+0

啊。我明白了,谢谢你。尼斯 – SavantCode

+0

啊我见。你是否看到@Turnip使用连字符而不是下划线来评论你?你应该使用'_email'而不是'-email'。或者将它们都改为连字符 –