JavaScript输入框更新问题? [英] JavaScript INPUT box update issue?

查看:75
本文介绍了JavaScript输入框更新问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Javascript对表单进行客户端验证.

但是,每个INPUT的验证将取决于所有其他INPUT的当前值.脚本的精简版本是:

I''m trying to do a client-side validation of a form using Javascript.

The validation for each INPUT will however depend on the current values of all other INPUTs. A trimmed down version of the script is:

// e.g. call function on an form INPUT
validateX(form.varx.value);

function validateX(xfv) {
// trimmed down version of function...
        var ux=10;
        // save the current value
        xtemp=xvf;
        // set to possible lower limit
        xfv=ux;
        // do calc based on all form INPUTs
        var y1=tempcalc(form);
        // further calculation to get true upper limit of INPUT based on y1
    }



问题在于"tempcalc"似乎使用了INPUT的OLD值,而不是使用"xfv = ux"分配的值.据我所知,直到函数返回时,该值才真正更新,因此在调用函数体内被调用的另一个函数看不到更改.

有什么想法可以解决这个问题吗?

干杯,
史蒂夫.



The problem is that ''tempcalc'' seems to use the OLD value of the INPUT, not the one assigned with ''xfv=ux''. As far as I can see the value isn''t actually updated until the function returns, so that another function called within the body of the calling function can''t see the change.

Any ideas how to get round this?

Cheers,
Steve.

推荐答案

您是正确的,另一个函数无法以编写函数的方式查看更新后的值.

而不是将值传递给validateX,而是传递控件并在validateX内操作ctl.value.

这是一个简单的测试用例:

You are correct, the other function can not see the updated value the way your function is written.

Instead of passing in the value to validateX, pass in the control and manipulate the ctl.value inside validateX.

Here is a simple test case:

<html>
<head>
    <script type="text/javascript">
        function test1(val) {
            alert("Value before assignment " + val);
            val = "fred";
            display();
        }
        function test2(ctl) {
            alert("Value before assignment " + ctl.value);
            ctl.value = "fred";
            display();
        }
        function display() {
            alert("Value after assignment " + form.tb.value);
        }
    </script>
<body>
    <form name="form" method="post"  id="aspnetForm">
        <input id="tb" type="text" value="Initial Value" />
        <input type="button" onclick="test1(form.tb.value);" value="do test1" />
        <input type="button" onclick="test2(form.tb);" value="do test2" />
    </form>
</body>
</html>


您的函数仅传递字符串值.请改用控件.

//例如表单INPUT上的调用函数
validateX(form.varx);

函数validateX(xfv)
{//精简版的功能...
var ux = 10;
//保存当前值
xtemp = xvf;
//设置为可能的下限
xfv.value=ux;
//根据所有形式的INPUT进行计算
var y1 = tempcalc(form);
//进一步计算,以基于y1获得真实的INPUT上限
}
Your function is only passing a string value. Use the control instead.

// e.g. call function on an form INPUT
validateX(form.varx);

function validateX(xfv)
{// trimmed down version of function...
var ux=10;
// save the current value
xtemp=xvf;
// set to possible lower limit
xfv.value=ux;
// do calc based on all form INPUTs
var y1=tempcalc(form);
// further calculation to get true upper limit of INPUT based on y1
}


这篇关于JavaScript输入框更新问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆