简体   繁体   English

通过“ id”而不是“ value”属性为表单输入分配值

[英]Assigning value to form input by "id” and not by “value” attribute

This is my current situation: 这是我目前的情况:

 document.body.addEventListener('click', calculateTotal); const calculator = document.querySelector("form"); function cake() { const cakes = Array.from(calculator.elements["cake"]).slice(0, 3); const raphael = calculator.elements["raphael"]; function isChecked(checkbox) { return checkbox.checked; } var count = cakes.filter(isChecked).length; if (count) { count = count * 0.5 + 0.5; } if (raphael.checked) { count += 1; } return count; } function occasion() { const occasionOptions = { birthday: 25, anniversary: 50, wedding: 100 } var occasionCost = 0; const occasion = calculator.elements["occasion"]; for (var i = 0; i < occasion.length; i++) { if (occasion[i].checked) { occasionCost = occasionOptions[occasion[i].value]; break; } } return occasionCost; } function calculateTotal() { var totalCost = cake() * occasion(); calculator.total.value = "$" + totalCost.toLocaleString("en"); } 
 <form> <fieldset> <legend>Select Cakes</legend> <label><input type="checkbox" name="cake" id="leonardo" required>Leonardo</label> <label><input type="checkbox" name="cake" id="donatello">Donatello</label> <label><input type="checkbox" name="cake" id="michelangelo">Michelangelo</label> <label><input type="checkbox" name="cake" id="raphael">Raphael</label> <p>If you select more than one cake, the other cakes are discounted 50%.</p> <p><small>Does not apply to Raphael.</small></p> </fieldset> <fieldset> <legend>Choose Occasion</legend> <label><input type="radio" name="occasion" value="birthday">Birthday</label> <label><input type="radio" name="occasion" value="anniversary">Anniversary</label> <label><input type="radio" name="occasion" value="wedding">Wedding</label> </fieldset> <input type="text" name="total"> </form> 

In the example above, I would like to switch value to id attribute on input radio . 在上面的示例中,我想在input radio上将value切换为id属性。 If I for example replace value="birthday" to id="birthday" , the values are no longer assigned to input . 例如,如果我将value="birthday"替换为id="birthday" ,则这些值将不再分配给input

What am I missing? 我想念什么? Any help would be greatly appreciated. 任何帮助将不胜感激。

(Code snippet updated to include both id and value in fieldsets.) (代码段已更新为在value集中包含idvalue 。)

You can use HTMLElement.id to get the id of an element. 您可以使用HTMLElement.id来获取元素的id

 document.body.addEventListener('click', calculateTotal); const calculator = document.querySelector("form"); function occasion() { const occasionOptions = { birthday: 2.5, anniversary: 5, wedding: 10 } var occasionCost = 0; const occasion = calculator.elements["occasion"]; for (var i = 0; i < occasion.length; i++) { if (occasion[i].checked) { occasionCost = occasionOptions[occasion[i].id]; break; } } return occasionCost; } function calculateTotal() { var totalCost = 100 * occasion(); calculator.total.value = "$" + totalCost.toLocaleString("en"); } 
 <form> <fieldset> <legend>Choose Occasion</legend> <label><input type="radio" name="occasion" id="birthday">Birthday</label> <label><input type="radio" name="occasion" id="anniversary">Anniversary</label> <label><input type="radio" name="occasion" id="wedding">Wedding</label> </fieldset> <input type="text" name="total"> </form> 

i do not know the rationale for using id property over value, but if that is what you want to do, all you need to do is to update your js to read id property instead of value property. 我不知道使用id属性代替值的理由,但是如果那是您想要做的,那么您要做的就是更新您的js以读取id属性而不是value属性。

https://codepen.io/anon/pen/XBvYoL https://codepen.io/anon/pen/XBvYoL

HTML: HTML:

<form>
<fieldset>
<legend>Choose Occasion</legend>
<label><input type="radio" name="occasion" id="birthday">Birthday</label>
<label><input type="radio" name="occasion" id="anniversary">Anniversary</label>
<label><input type="radio" name="occasion" id="wedding">Wedding</label>
</fieldset>

<input type="text" name="total">
</form>

JS: JS:

document.body.addEventListener('click', calculateTotal);

const calculator = document.querySelector("form");

function occasion() {
 const occasionOptions = {
  birthday: 2.5,
  anniversary: 5,
  wedding: 10
 }

 var occasionCost = 0;
 const occasion = calculator.elements["occasion"];

 for (var i = 0; i < occasion.length; i++) {
  if (occasion[i].checked) {
   occasionCost = occasionOptions[occasion[i].id];
   break;
  }
 }
 return occasionCost;
}

function calculateTotal() {
 var totalCost = 100 * occasion();
 calculator.total.value = "$" + totalCost.toLocaleString("en");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM