javascript - 需要帮助将 jQuery 函数转换为 JavaScript?

标签 javascript html

我正在更新产品,我可以让产品信息预填充更新表单并使用 jQuery 更新产品信息,但我想使用 JavaScript。如何将此 jQuery 代码转换为 JavaScript?

我正在遵循在线教程,该人正在使用 jQuery,这很酷,但我想了解如何使用 Javascript 执行相同的方法。

Javascript代码:

$(document).on('pagebeforeshow', '#updatedialog', function() {
  $('#newName').val(currentProduct.name);
  $('#newQuantity').val(currentProduct.quantity);
});

function updateProduct() {
  var newName = $('#newName').val();
  var newQuantity = $('#newQuantity').val();
  productHandler.updateProduct(currentProduct.id, newName, newQuantity);
}

HTML 更新表单

<form>
  <div class="ui-field-contain">
    <label for="newName"
           class="ui-hidden- accessible">Name</label>
    <input type="text"
           id="newName"
           data-clear-btn="true"
           placeholder="New Name"/>
    <br/>

    <label for="newQuantity"
           class="ui-hidden-accessible">Quantity</label>
    <input type="number" 
           name="number" 
           pattern="[0-9}"
           id="newQuantity" 
           value="" 
           data-clear-btn="true" 
           placeholder="New Quantity"/>
    <br/>
    <button class="ui-btn ui-icon-plus ui-btn-icon-left"
            id="btnupdate"
            onclick="updateProduct()">
      Update
    </button>
  </div>
</form>

更新表单应填充已输入的产品信息,然后更新对字段所做的更改并将其保存为新对象。我可以在 jQuery 中完成此操作,但我需要在 Javascript 中完成此操作的帮助。

最佳答案

看来您当前使用 jquery 所做的只是通过 ID 获取输入元素的值。您可以使用 JavaScript 通过 ID 选择表单元素并获取 value 属性来完成此操作。

val value = document.getElementById("elemID").value;

你的代码应该是这样的

 function updateProduct(){
 var newName= document.getElementById("newName").value;
 var newQuantity = document.getElementById("newQuantity").value;
 productHandler.updateProduct(currentProduct.id, newName, newQuantity);
  }

关于javascript - 需要帮助将 jQuery 函数转换为 JavaScript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54327686/

相关文章:

javascript - 在新上下文中使用 'super' 的最佳方法是什么?

javascript - 禁用浏览器请求 "store credit Card information"

html - 输入类型 ="image"未在 IE 中加载

javascript - history.pushState 后转发按钮不起作用

javascript - 将处理消息放在具有现有数据的数据表的表顶部?

javascript - 如何从 webpack 加载器访问 DOM

javascript - 使用 jquery 清除元素的属性

javascript - setState 使用 File onLoad

javascript - 如何在 JavaScript 中将一年中的某一天转换为日期?

javascript - 如何将事件的 css 类添加到 Wordpress 中的链接