javascript - 使用 jQuery、laravel 5.3 在提交时将表单数据发送到 div,在表单外

标签 javascript php jquery forms laravel

这是我的表格

      <form method="post" action="{{url('/vpage')}}"> 
      <input type="hidden" name="_token" value="{{ csrf_token() }}">

      <label>First Name</label>
           <input type="text" name="firstname" placeholder="First Name" value="{{$user->firstname}}" >

      <label>Email Address</label>
          <input type="text" name="email" placeholder="Email Address"  value="{{$user->email}}" >

      <label>Phone Number <span> (optional)</span></label>
             <input type="text" name="phone" placeholder="(888) 888-888" value="{{$user->phone}}" >

      <button id="hitme" class="submitBTN getstart" type="submit" onclick='return false;'> Get Started </button> 
       </form> 

这是表单外的div

   <div class="vgasRit">
        <p>SUMMARY</p>
           <div class="sfieldz w100">

              <label>Name:</label>
              <input type="text" placeholder="John Smith" value="{{$user->firstname}}">

          </div>
          <div class="w100">
              <label>Email Address:</label>
              <input type="text" placeholder="johnsmith@gmail.com" value="{{$user->email}}" >
           </div>

            <div class="w100">
               <label>Phone Number:</label>
            <input type="text" placeholder="(888) 888-888" value="{{$user->phone}}">
             </div>
    </div>

我想在用户提交表单时访问“fistname”、“lastname”和“phone”的值,以便我可以在摘要 div 中显示它。

注意: 我已经在我的 Controller 中尝试了 php 的紧凑功能,这样我就可以在我的 View 中发送整个数据库对象,但是这个解决方案不起作用,在使用紧凑功能之后我访问了对象这个

    <input type="text" placeholder="John Smith"  value="<?= (!empty($group_data)) ? $group_data->firstname : '';?>">

对此有什么想法吗?我是 laravel 的新手。我在互联网上服务了几个小时,但一无所获。

最佳答案

假设一切都在同一个页面上,给你的表单输入一个id:

<form id="form" method="post" action="{{url('/vpage')}}"> 
<label>First Name</label>
       <input id="firstname" type="text" name="firstname" placeholder="First Name" value="{{$user->firstname}}" >

  <label>Email Address</label>
      <input id="email" type="text" name="email" placeholder="Email Address"  value="{{$user->email}}" >

  <label>Phone Number <span> (optional)</span></label>
         <input id="phone" type="text" name="phone" placeholder="(888) 888-888" value="{{$user->phone}}" >

也给你的摘要 div 输入一个 id:

<p>SUMMARY</p>
       <div class="sfieldz w100">

          <label>Name:</label>
          <input id="firstname2" type="text" placeholder="John Smith" value="{{$user->firstname}}">

      </div>
      <div class="w100">
          <label>Email Address:</label>
          <input id="email2" type="text" placeholder="johnsmith@gmail.com" value="{{$user->email}}" >
       </div>

        <div class="w100">
           <label>Phone Number:</label>
        <input id="phone2" type="text" placeholder="(888) 888-888" value="{{$user->phone}}">
         </div>

然后,在用户提交表单时使用 jquery 获取这些值:

$('#form').submit(function() {
    // set our summary div inputs values with our form values
    $('firstname2').val($('firstname').val());
    $('email2').val($('email').val());
    $('phone2').val($('phone').val());
});

应该是这样吧。

关于javascript - 使用 jQuery、laravel 5.3 在提交时将表单数据发送到 div,在表单外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42510585/

相关文章:

javascript - WebRTC connectionState 卡在 "new"- 仅适用于 Safari,适用于 Chrome 和 FF

javascript - React Native AsyncStorage 返回 promise 而不是值(value)

php - Laravel - 如何向浏览器提供新的 Laravel 项目

javascript - 如何根据th更新td?

javascript - javascript 中的字符串搜索无法找到当前字符串

javascript - 如何将生产网站的Javascript替换为本地Javascript?

php - 格式化 json 数据以在 Flot 中使用

php - 如何优化导入/导出数据的查询,从 2 个表到 1 个表,记录超过 200k

javascript - POST 后维护 View 状态

javascript - 动态 float 气泡..使用Jquery