如何在单击时更改文本字段的值
How do I change the value of a text field on click?
我正在制作一个PHP计算器。我一直遇到的一个问题是:
<input type="button" name="insert" value="0" style="width: 30px; height: 35px" onClick="zeroCheck()">
如果有一个只包含"0"的文本域,我希望它只是用另一个"0"替换这个0,而不是在它上面添加另一个"0"。
我的文本字段名为entry。
为了解决这个问题,我尝试开发这个函数,但是没有成功。
function zeroCheck(){
if (entry.value == '0'){
entry.value=='0';
} else{
return entry.value+='0';
}
}
这根本没有做任何事情。我哪里做错了?
如果有人能帮忙的话,我将不胜感激。
我能想到的最简单的解决方案是:
function zeroCheck(el) {
// caching the value because it's used multiple times
var v = el.value;
/* setting the value with a ternary/conditional operator:
v == '0' : testing that the value is equal to the given string
if it is equal: the value is set to: '' + v (so there's no change),
if it is not equal: the value is set to: '0' + v (so a zero is prepended).
*/
el.value = ( v == '0' ? '' : '0' ) + v;
}
JS Fiddle demo.
在链接的演示中,上面的JavaScript与以下HTML一起使用:<input type="button" name="insert" value="0" onClick="zeroCheck(this)" />
<input type="button" name="insert" value="1" onClick="zeroCheck(this)" />
<input type="button" name="insert" value="2" onClick="zeroCheck(this)" />
<input type="button" name="insert" value="3" onClick="zeroCheck(this)" />
添加一些(基本的)错误处理:
function zeroCheck(el) {
// caching the value, because we're using it a few times
var v = el.value,
// saving the number (this way 1.4 and 1 are both preserved)
num = parseFloat(v);
// a basic check to see that the value is a number
// (or rather that it is 'not Not-a-Number):
if (!isNaN(num)) {
el.value = ( v == '0' ? '' : '0' ) + v;
}
else {
// default handling here, to handle non-numeric values:
el.value = 0;
}
}
JS Fiddle demo.
因为我认为我误解了这个问题,我用另一种可能性更新了这个答案,它处理兄弟文本输入元素,并且还考虑到已经具有前导零的值:
function zeroCheck(el) {
/* gets all the 'input' elements within the same parentNode that
contains the button that's to be clicked: */
var inputs = el.parentNode.getElementsByTagName('input'),
/* declaring the variables to be used in the loop, so they're not
being constantly re-declared through the loop */
v, num;
for (var i = 0, len = inputs.length; i < len; i++) {
v = inputs[i].value;
num = parseFloat(v);
// if the input is the button, don't do anything, just keep going
if (inputs[i] == el) {
continue;
// otherwise if the number is not Not-a-Number:
} else if (!isNaN(num)) {
inputs[i].value = (v.charAt(0) == '0' ? '' : '0') + v;
} else {
inputs[i].value = 0; // error-handling
}
}
}
JS提琴演示
好了,因为这和我想象的有很大的不同,让我们把这里弄清楚,让我们简化这个过程。如果函数在PHP中,你将无法设置,甚至无法获得,input
控件的值。在PHP文件中,您需要做这样的事情(请注意,我假设form
是POST
):
$entryVal = $_POST["entry"];
$newEntryVal = "0";
if (trim($entryVal) != "0") {
$newEntryVal = $newEntryVal + "0";
}
,然后在后面的标记中:
<input type="text" name="entry" size="20" value = "<?= $newEntryVal ?>">
最好给你的输入一个id:
<input type="button" id="someid" name="insert" value="0" style="width: 30px; height: 35px" onClick="zeroCheck()">
function zeroCheck(){
var entry = document.getElementById("someid");
if (entry.value == '0'){
entry.value = '0';
} else {
entry.value += '0';
}
}
有很多东西是缺失的。下面是正确的代码:
function zeroCheck(){
var entry = document.getElementsByName("entry")[0];
if (entry.value == '0'){
entry.value = parseInt('0');
} else {
entry.value += parseInt('0');
}
}
不需要返回,您正在使用==
,因为应该是=
来赋值。还有一个问题是你想用entry.value += '0';
代码做什么
相关文章:
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 当设置addFromAutocompleteOnly时,剩余文本将保留在输入字段中
- 使用jquery将输入字段转换为文本
- 如何从查询字符串中的输入字段发回文本
- 表单输入字段随着溢出的文本而增长
- 使用单个文本框向多个字段添加严格搜索
- 使用javascript提交表单并从字段/单选框/复选框/文本区域获取数据
- 使用jQuery的输入字段文本换行
- 向使用jQuery加载DOM后添加的字段添加不受限制的文本输入DatePickers
- 当用户单击按钮(在光标位置)时,在输入字段中添加一个文本字符串
- 如何根据文本长度立即显示和隐藏字段?-JQuery
- 如何使文本、数字和日期html输入字段以一致的方式支持Ctrl+Z(撤消)
- 模拟文本输入字段上的退格键
- 输入字段中的可持续文本
- 未获取文本输入字段的值
- 没有文本安全性的密码字段
- 将禁用的输出字段更改为带前缀的文本
- 为什么 react 将输入[类型=“文本”] 字段设为只读,除非我提供 onChange 回调
- 使用SAPUI5中的“文本”字段显示正确的“日期”
- Kendo,如何将MVVM文本:字段绑定到远程DataSource