简体   繁体   中英

How to dynamically keep input field empty when select box option is no?

Ì have form where writing on input field autofills other input field while user types email

$("#edit-submitted-yhteystiedot-sahkoposti").keyup(function(){
    $("#edit-submitted-saannot-newsletter-newsletter-email-address").val(this.value);
});

The thing is that if user does not want to receive newsletter and chooses no option for select box, the Input field with ID #edit-submitted-saannot-newsletter-newsletter-email-address should remain empty, even if user makes changes to default email field. ID of SELECT is #edit-submitted-saannot-haluan-uutiskirjeen

var $select = $('#edit-submitted-saannot-haluan-uutiskirjeen'),
    $emailInput = $("#edit-submitted-saannot-newsletter-newsletter-email-address");
$select.change(function(){
    if($select.val() == 'No')
        $emailInput.val('');
}):
$emailInput.keyup(function(){
    if($select.val() == 'No')
        $emailInput.val('');
});

When the user changes the select to "no" reset the email field and set a custom attribute to know that you don't want automatic updates

$("#edit-submitted-saannot-newsletter-newsletter-email-address").val(this.value).attr("noautoupdate", true);

Then change your code like this

$("#edit-submitted-yhteystiedot-sahkoposti").keyup(function(){
  if (!$(this).attr("noautoupdate")) $("#edit-submitted-saannot-newsletter-newsletter-email-address").val(this.value);
});

If the user changes the select to "yes" make the attribute false:

$("#edit-submitted-saannot-newsletter-newsletter-email-address").val(this.value).attr("noautoupdate", false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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