[英]How to use global variable so i can use it on my javascript module
i want to get all of this input values to my budget app but i have problem to get values of the radio button because it says its undefined.我想将所有这些输入值添加到我的预算应用程序中,但我无法获取单选按钮的值,因为它说它未定义。 i create global function to get by radio button value.
我创建全局 function 以通过单选按钮值获取。 but the others is in javascript module.
但其他的是 javascript 模块。
https://jsfiddle.net/8k3gw7ty/ https://jsfiddle.net/8k3gw7ty/
<div class="button_income">
<input type="radio" name="type" value="inc" id="incomebtn" onclick="getButtonValue();" checked>
<label for="incomebtn" class="income-btn">+ Add Income</label>
</div>
<div class="button_expense">
<input type="radio" name="type" value="exp" id="expensebtn" onclick="getButtonValue();">
<label for="expensebtn" class="expense-btn">+ Add Expense</label>
</div>
<div class="desc_input">
<label class="labelinput" for="input-desc">Your Income/Expense Description</label>
<input id="input-desc" type="text" class="input_description" placeholder="Salary">
</div>
<div class="value_input">
<label class="labelinput" for="input-val">Value of Income/Expense</label>
<input id="input-val" type="number" class="input_value" placeholder="Rp. 100.000">
</div>
Actually there was no default value for your val
variable.实际上,您的
val
变量没有默认值。 Since val
will only get value when you click on the checkbox (according to your code).因为
val
只有在您单击复选框时才会获得价值(根据您的代码)。
Also you were returning val
which isn't necessary.您还返回了不必要的
val
。 I've also removed the budgetController
.我还删除了
budgetController
。
Hope this'll help.希望这会有所帮助。
let val = 'inc'; // default value function getButtonValue() { var type = document.getElementsByName("type"); if (type[0].checked) { val = type[0].value } else if (type[1].checked) { val = type[1].value } } const domController = (function() { return { getInput: function() { return { type: val, description: document.querySelector(".input_description").value || 0, value: parseFloat(document.querySelector(".input_value").value) || 0 } } } })(); const controller = (function( UI) { var ctrlAddItem = function() { var input = UI.getInput(); console.log(input); } document.querySelector(".addbtn").addEventListener("click", ctrlAddItem) document.addEventListener("keypress", function(event) { if (event.keyCode === 13 || event.which === 13) { ctrlAddItem(); } }); })( domController);
<div class="button_income"> <input type="radio" name="type" value="inc" id="incomebtn" onclick="getButtonValue();" checked> <label for="incomebtn" class="income-btn">+ Add Income</label> </div> <div class="button_expense"> <input type="radio" name="type" value="exp" id="expensebtn" onclick="getButtonValue();"> <label for="expensebtn" class="expense-btn">+ Add Expense</label> </div> <div class="desc_input"> <label class="labelinput" for="input-desc">Your Income/Expense Description</label> <input id="input-desc" type="text" class="input_description" placeholder="Salary"> </div> <div class="value_input"> <label class="labelinput" for="input-val">Value of Income/Expense</label> <input id="input-val" type="number" class="input_value" placeholder="Rp. 100.000"> </div> <button><i class="fas fa-check addbtn">Save</i></button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.