简体   繁体   English

表单提交后在同一成功消息中显示两个 id#

[英]display two id# in same success message after form submit

i am trying to display text field val along with the val of a select field in a success message after form has submitted我正在尝试在提交表单后在成功消息中显示文本字段 val 以及 select 字段的 val

This retuns the select field val.这将返回 select 字段值。 how would i store and add another val in same string我将如何在同一个字符串中存储和添加另一个 val

<script>
// when the DOM is ready
$(document).ready(function() {
  // store a reference to the select field 
  const $budget = $('#budget');
  // store budget
  const $bookingForm = $('#booking-form');
  // store success text field in the $successText variable
  const $successText = $('.insert-success-text');
  
  let customSuccessMessage = $budget.val();

  // select field changes
  $budget.change(function(){
    // assign the new selected option
    customSuccessMessage = $(this).val();
  });

  // when the form's submit button is clicked 
  $bookingForm.submit(function(e){
     // if 
    if(customSuccessMessage){
      // find .insert-success-text and add this text 
      $successText.text(`Thank you 'Thank you **text field val*** for your intrest in a ${customSuccessMessage}`);
      // then submit the form
      return true;
    }
    else{   // else if no option was selected
      // focus on the select field
      $budget.focus();
      // stop form submission
      return false;
    }
  });  
});
</script>

I modified the code above.我修改了上面的代码。

<script>
$(document).ready(function() {
  const $budget = $('#budget');
  const $textField = $('#text-field');
  const $bookingForm = $('#booking-form');
  const $successText = $('.insert-success-text');
  
  let customSuccessMessage = $budget.val();

  $budget.change(function(){
    customSuccessMessage = $(this).val();
  });
  $bookingForm.submit(function(e){
    if(customSuccessMessage){
     
      $successText.text(`Thank you ${$textField.val()} for your interest in a ${customSuccessMessage}`);
      return true;
    }
    else{   
      $budget.focus();
      return false;
    }
  });  
});
</script>

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

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