首页 > 解决方案 > document.getElementById("") = "" 在 Laravel 刀片中不起作用

问题描述

我正在寻找一种在表单中设置“价值”的方法,我试过这个,但它不起作用。我应该如何解决它?

错误说

未捕获的 ReferenceError:setData 未在 HTMLInputElement.onclick 中定义

但是 setDate() 肯定存在....

<script>
    function setData() {
        document.getElementById("year").value = "2022";
    }
</script>

<div>
    <form method="GET" action={{ route('test')}}>
        {{ Form::select('month', $date, 0 ) }}
        <input type="button" id="b1" value="BUTTON1" onclick="setData()">
    </form>
</div>

<form method="post" action={{ route('output')}}>
    @csrf
    <div class="col-md-6">
        <input type="text" id="year" name="year" value ="2021"> 
        <input type="text" name="month" value ="7"> <input type="text" name="day" value ="1"> 
    </div>
</form>

标签: javascriptphplaravel

解决方案


试试下面的脚本,

在您的第二个表单中添加名称

<form method="GET" action="{{ route('test')}}">
      <select name="" id="month">
          <option value="">Select</option>
          <option value="jan">Jan</option>
          <option value="feb">Feb</option>
      </select>
      <input type="button" id="b1" value="BUTTON1" onclick="setData()">
</form>

<form method="post" id="form2" action="{{ route('output')}}">
     @csrf
     <div class="col-md-6">
          <input type="text" id="year" name="year" value ="2021">
          <input type="text" name="month" value ="7"> <input type="text" name="day" value ="1">
     </div>
</form>

<script>
     var secondForm = document.getElementById('form2');
     
     function setData() {
         secondForm.year.value = '2022';
     }
</script>

推荐阅读