从打字稿中的<input>元素获取值

Fan*_*onX 3 javascript typescript

我目前正在尝试获取用户将插入输入表单的值。在普通javascript中,我可以按id或class等来定位元素,然后可以使用.value方法来实际使用该方法。由于某种原因,打字稿无法做到这一点,我不理解,因为打字稿是javascript的超集。是否有从纯打字稿中的输入元素获取值的特定方法,还是我必须使用angular或其他工具?

打字稿代码:

interface IUserFoodOptions {
    food: string;
    calories: number;
    foodDate: any;
}

class Food implements IUserFoodOptions {
    food: string;
    calories: number;
    foodDate: any;

    // store all the calories in an array and calculate the total amount of calories
    caloriesArray: number[] = [];

    // constructor
    constructor(food: string, calories: number, foodDate: any) {
        this.food = food;
        this.calories = calories;
        this.foodDate = foodDate;
    }
}

// event listener for when the add food button is clicked
let addFood = document.getElementById("add-food-button").addEventListener("click", () => {
    // get the values from inputs and store them in an array
    let foodName = document.getElementById("food-name-val");
    let foodCalories = document.getElementById("calories-val");
    let dateVal = document.getElementById("date-val");

    // store these values in a list and display them below
    // user will have the ability to edit and delete them
    // am I create event listeners within event listeners
});
Run Code Online (Sandbox Code Playgroud)

小智 12

是的,TypeScript 有这个“小问题”,但这是为了安全。
您可以通过以下方式获取输入值:

var inputValue = (<HTMLInputElement>document.getElementById(elementId)).value;
Run Code Online (Sandbox Code Playgroud)

你可以在这里看到更多关于这个casting <>的东西:TypeScript:casting HTMLElement

希望它有效!


ako*_*liy 7

您可以在 TypeScript 中获取输入的值。对于一个数,

var num = parseFloat((<HTMLInputElement>document.getElementById("myValue")).value);
                                 or 
let num : number = parseFloat((<HTMLInputElement>document.getElementById("myValue")).value);
Run Code Online (Sandbox Code Playgroud)

字符串;

var str = (<HTMLInputElement>document.getElementById("myUnit")).value; 
         or
let str : string = (<HTMLInputElement>document.getElementById("myUnit")).value; 
Run Code Online (Sandbox Code Playgroud)

将 HTMLElement 转换为 HTMLInputElement 很重要,否则 TypeScript 中的 HTMLElement 不存在 'value' 属性,并且 TypeScript 编译器将显示错误。

// event listener for when the add food button is clicked
let addFood = document.getElementById("add-food-button").addEventListener("click", () => {
  // get the values from inputs and store them in an array
  let foodName = (<HTMLInputElement>document.getElementById("food-name-val")).value;
  let foodCalories = parseFloat((<HTMLInputElement>document.getElementById("calories-val")).value);
  let dateVal = (<HTMLInputElement>document.getElementById("date-val")).value;
  // And so on ...
});
Run Code Online (Sandbox Code Playgroud)


pei*_*ent 6

如果检查了正在调用的方法后面的类型文件getElementById,则将看到它返回一个HTMLElement。此类型没有value属性。

不过,所有HTML元素都继承自这一元素。为了使它起作用,您只需要让Typescript知道您期望选择的元素类型。您可以按以下步骤进行操作:<HTMLInputElement> document.getElementById("food-name-val")然后可以按以下方式访问该value属性:let foodName = (<HTMLInputElement> document.getElementById("food-name-val")).value;

  • 奇迹般有效 :) (3认同)