简体   繁体   English

jQuery从不同事件中的变量访问更新后的值

[英]jQuery accessing updated values from variables on different events

I have a function which copies the values of a group of inputs to another group of inputs if the user clicks a button. 我有一个函数,如果用户单击按钮,该函数会将一组输入的值复制到另一组输入。

The function works fine but I'm having trouble with the vars I'm using to get the information. 该函数工作正常,但我在获取信息时使用的var遇到了麻烦。 I want to use global variables because I want to use the same variables later on but it only works when I wrap those vars inside the function. 我想使用全局变量,因为稍后我想使用相同的变量,但是它仅在将这些var包装在函数中时才起作用。

I've posted the two scenarios where it's working and not working below. 我在下面发布了两种可行且无法正常工作的方案。 Can anyone explain why I cannot access the correct value at that given time using a global variable? 谁能解释为什么我不能使用全局变量在给定的时间访问正确的值?

EDITS: The 3 elements #admin-name, #admin-email and #admin-number are all present in the DOM when the script is called, as I am doing everything with document ready. 编辑:调用脚本时,DOM中存在#admin-name,#admin-email和#admin-number这3个元素,因为我在准备文档的同时进行了所有操作。 I understand that when the script first runs these values will be blank because they haven't been filled out by the user yet. 我了解脚本首次运行时,这些值将为空白,因为用户尚未填写它们。 What I don't understand is why can't jQuery get the value once it has been filled out and I call the variable on the click function. 我不明白的是,为什么jQuery在填写完毕后就无法获取该值,而我却在click函数上调用了该变量。

Not Working 无法运作

var contactName = $('#contact-name').val();
var contactEmail = $('#contact-email').val();
var contactNumber = $('#contact-number').val();

$(".step-two .copy-details").on('click', function(){
    $('#admin-name').val(contactName);
    $('#admin-email').val(contactEmail);
    $('#admin-number').val(contactNumber);
  });

Working 工作中

$(".step-two .copy-details").on('click', function(){
    var contactName = $('#contact-name').val();
    var contactEmail = $('#contact-email').val();
    var contactNumber = $('#contact-number').val();
    $('#admin-name').val(contactName);
    $('#admin-email').val(contactEmail);
    $('#admin-number').val(contactNumber);
  });

Man I struggled with this one, this post helped flesh it out for me, jQuery Global Variable . 我为此奋斗的人,这篇文章为我jQuery Global Variable充实了它。 The problem is the variable called in the click function was still getting the original value of 0. To make the variable update when a new value is added you need to declare it and then wrap it in a change function like so: 问题是在click函数中调用的变量仍然获得原始值0。要在添加新值时使变量更新,您需要对其进行声明,然后将其包装在change函数中,如下所示:

JS JS

// declare the variable
var contactName;

// wrap it in a change function so the value updates
$('#contact-name').on('change', function(){
    contactName = $('#contact-name').val();
});

// use the variable inside the function and it will show the updated value
$('.step-two').on('click', 'button', function(){
    $('#admin-name').val(contactName);
    console.log('contactName = ' + contactName);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM