亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將通過函數使用按鈕輸入的內容返回到index.html?

如何將通過函數使用按鈕輸入的內容返回到index.html?

哈士奇WWW 2023-12-14 17:07:16
這就是我得到的,當在輸入框中輸入內容時,該項目會推送到數組,然后單擊按鈕提交,我似乎無法將其顯示在 html 上,不確定我哪里出錯了$( document ).ready ( readyNow );let garage = [];function readyNow() {  console.log( 'JQ' );  $( '#addCarButton' ).on( 'click', addCar )} //end readyNowfunction addCar() {  console.log('in addCar');  //get unser inputs  //create new object  let newCars = {    year: $( '#yearInput' ).val(),    make: $( '#makeInput' ).val(),    model: $( '#modelInput' ).val()  }  //push the new car into the array  garage.push( newCars );  //empty inputs  $( '#yearInput' ).val( '' );  $( '#makeInput' ).val( '' );  $( '#modelInput' ).val( '' );}console.log(garage);function displayGarage(){  console.log('in displayGarage');  $('#garageOut ').append      ( '<li> Year: ' + newCars.year +             'Make: ' + newCars.make +             'Model: ' + newCars.model +'</li>');  }<!DOCTYPE html><html>  <head>    <meta charset="utf-8">    <script src="scripts/jQuery.js" charset="utf-8"></script>    <script src="scripts/client.js" charset="utf-8"></script>    <link rel="stylesheet" href="styles/style.css">    <title>Week 6 Tier 1 Assignment</title>  </head>  <body>  <h1><img src="images/logo.png" alt="noah's car garage"></h1>      <h2>Please Enter your Year, Make, and Model: <span id="garageList"></span></h2>    <input placeholder="Year" id="yearInput" />    <input placeholder="Make" id="makeInput" />    <input placeholder="Model" id="modelInput" />    <button id= "addCarButton">Add Car</button>    <h3>Garage:</h3><div id ="garageOut"></div></div>  </body></html>請幫忙,我可以看到當輸入按鈕時數組在控制臺中輸出,但我的 html 上沒有顯示任何內容,我是否沒有以某種方式獲取它?我在 html 的 div 上將 id 設置為 GarageOut
查看完整描述

1 回答

?
撒科打諢

TA貢獻1934條經驗 獲得超2個贊

  1. displayGarage添加新車后您不再打電話。

  2. 僅僅調用它是不夠的,你需要傳遞一個新車給它

$(document).ready(readyNow);


let garage = [];


function readyNow() {

  console.log('JQ');

  $('#addCarButton').on('click', addCar)

} //end readyNow


function addCar() {

  console.log('in addCar');

  //get unser inputs

  //create new object

  let newCar = {

    year: $('#yearInput').val(),

    make: $('#makeInput').val(),

    model: $('#modelInput').val()

  }

  //push the new car into the array

  garage.push(newCar);

  //empty inputs

  $('#yearInput').val('');

  $('#makeInput').val('');

  $('#modelInput').val('');


  displayGarage(newCar); // NEW

}

console.log(garage);




function displayGarage(newCar) { // NEW

  console.log('in displayGarage');


  $('#garageOut ').append('<li> Year: ' + newCar.year +

    'Make: ' + newCar.make +

    'Model: ' + newCar.model + '</li>');

}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<!DOCTYPE html>

<html>


<head>

  <meta charset="utf-8">

  <script src="scripts/jQuery.js" charset="utf-8"></script>

  <script src="scripts/client.js" charset="utf-8"></script>

  <link rel="stylesheet" href="styles/style.css">


  <title>Week 6 Tier 1 Assignment</title>

</head>


<body>


  <h1><img src="images/logo.png" alt="noah's car garage"></h1>


  <h2>Please Enter your Year, Make, and Model: <span id="garageList"></span></h2>

  <input placeholder="Year" id="yearInput" />

  <input placeholder="Make" id="makeInput" />

  <input placeholder="Model" id="modelInput" />

  <button id="addCarButton">Add Car</button>



  <h3>Garage:</h3>

  <div id="garageOut"></div>

  </div>

</body>


</html>


查看完整回答
反對 回復 2023-12-14
  • 1 回答
  • 0 關注
  • 139 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號