JS 或 jQuery 更改表单对象的值时出现问题

发布于 2024-11-09 10:57:14 字数 1178 浏览 0 评论 0原文

我正在使用 jQuery Mobile 和 Phonegap 创建一个学习计时器,并尝试通过 JS 更改表单值。

表单值:

 <form action="javascript:void(null);" id="theTimer" name="theTimer">
     <input id="timerDisplay" type='text' name="theTime" readonly/>
     <br />
     <input type='submit' name="start" value="Start" onclick="Start()"/>
     <input type='submit' name="stop" value="Stop" onclick="Stop()"/>
     <input type='submit' name="pause" value="Pause" onclick="Pause()"/>
  </form>

JS 代码:

function Pause() {
    if (timerID) {
        clearTimeout(timerID);
    }
    if (myTimer) {
        clearInterval(myTimer);
    }
    if (document.theTimer.pause.value == "Pause") {
        document.theTimer.pause.value = "Resume";
    } else {
        document.theTimer.pause.value = "Pause";
        tToday = new Date();
        total_time = tToday.getTime() - tDiff;
        tStart = new Date(total_time);
        timerID = setTimeout(UpdateTimer, 1000);
        startTimer();
    }
}

问题是,当我使用另一个框架时,表单值 Pause 会更改为 Resume 并通过 Pause() 函数继续暂停...现在它在以下位置看不到 Pause 表单值所有...

我缺少什么来获得表单值“暂停”以更改为“恢复”?

I am using the jQuery Mobile and Phonegap to create a Study Timer and I am trying to change a form value through JS.

The form value:

 <form action="javascript:void(null);" id="theTimer" name="theTimer">
     <input id="timerDisplay" type='text' name="theTime" readonly/>
     <br />
     <input type='submit' name="start" value="Start" onclick="Start()"/>
     <input type='submit' name="stop" value="Stop" onclick="Stop()"/>
     <input type='submit' name="pause" value="Pause" onclick="Pause()"/>
  </form>

The JS code:

function Pause() {
    if (timerID) {
        clearTimeout(timerID);
    }
    if (myTimer) {
        clearInterval(myTimer);
    }
    if (document.theTimer.pause.value == "Pause") {
        document.theTimer.pause.value = "Resume";
    } else {
        document.theTimer.pause.value = "Pause";
        tToday = new Date();
        total_time = tToday.getTime() - tDiff;
        tStart = new Date(total_time);
        timerID = setTimeout(UpdateTimer, 1000);
        startTimer();
    }
}

The problem is that when I was using another framework the form value Pause would change to Resume and Resume to pause through the Pause() function... now it doesn't see the Pause form value at all...

What am I missing to get to form value Pause to change to Resume?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

想挽留 2024-11-16 10:57:14

如果你使用jquery,你可以这样做:
if ($('input[name=pause]').val() == "暂停") {
$('input[name=pause]').val("继续");
}

而不是

if (document.theTimer.pause.value == "Pause") {
document.theTimer.pause.value = "恢复";
}

If you are using jquery, you could be doing:
if ($('input[name=pause]').val() == "Pause") {
$('input[name=pause]').val("Resume");
}

instead of

if (document.theTimer.pause.value == "Pause") {
document.theTimer.pause.value = "Resume";
}

你在我安 2024-11-16 10:57:14

函数中变量的范围令人困惑,因为您省略了所有变量的 var 关键字。我会首先清理你的变量声明,并使用 jQuery 的强大功能,而不是手动通过 DOM 来设置值:

 <form action="javascript:void(null);" id="theTimer" name="theTimer">
     <input id="timerDisplay" type="text" name="theTime" readonly="readonly" />
     <br />
     <input type="submit" name="start" id="start" value="Start"/>
     <input type="submit" name="stop" id="stop" value="Stop"/>
     <input type="submit" name="pause" id="pause" value="Pause"/>
  </form>

我删除了你的 onclick 事件并修复了你的 readonly 属性和你的引号。我认为部分问题是您在单击按钮时提交表单并且页面回发。当发生这种情况时,您的计时器正在重置,导致意外行为。我正在使用 jQuery 添加回函数:

$(function() {
    $('#start').click(function() {
        Start();
        return false;
    });
    $('#stop').click(function() {
        Stop();
        return false;
    });
    $('#pause').click(function() {
        Pause();
        return false;
    });
});

var timerID, myTimer;

function Pause() {
    if (timerID) {
        clearTimeout(timerID);
    }
    if (myTimer) {
        clearInterval(myTimer);
    }
    if ($('#pause').val() == "Pause") {
        $('#pause').val("Resume");
    } else {
        $('#pause').val("Pause");
        var tToday = new Date();
        var total_time = tToday.getTime() - tDiff;
        var tStart = new Date(total_time);
        timerID = setTimeout(UpdateTimer, 1000);
        startTimer();
    }
}

也许您可以用 startTimer()UpdateTimer() 的作用来填补问题中的空白?另外,您还没有真正解释您的问题到底是什么......您能原谅我们吗?

The scope of the variables within your function is confusing because you've omitted the var keyword on all of them. I would first clean up your variable declarations, and use the power of jQuery instead of manually fighting your way through the DOM to set values:

 <form action="javascript:void(null);" id="theTimer" name="theTimer">
     <input id="timerDisplay" type="text" name="theTime" readonly="readonly" />
     <br />
     <input type="submit" name="start" id="start" value="Start"/>
     <input type="submit" name="stop" id="stop" value="Stop"/>
     <input type="submit" name="pause" id="pause" value="Pause"/>
  </form>

I removed your onclick events and fixed your readonly attribute, and your quotes. I think part of the problem is that you were submitting the form when clicking the buttons and the page was posting back. Your timers were resetting when this happened causing unexpected behavior. I'm adding back the functions using jQuery:

$(function() {
    $('#start').click(function() {
        Start();
        return false;
    });
    $('#stop').click(function() {
        Stop();
        return false;
    });
    $('#pause').click(function() {
        Pause();
        return false;
    });
});

var timerID, myTimer;

function Pause() {
    if (timerID) {
        clearTimeout(timerID);
    }
    if (myTimer) {
        clearInterval(myTimer);
    }
    if ($('#pause').val() == "Pause") {
        $('#pause').val("Resume");
    } else {
        $('#pause').val("Pause");
        var tToday = new Date();
        var total_time = tToday.getTime() - tDiff;
        var tStart = new Date(total_time);
        timerID = setTimeout(UpdateTimer, 1000);
        startTimer();
    }
}

Perhaps you could fill in the gaps in your question with what startTimer() and UpdateTimer() do? Also, you haven't really explained what your issue is exactly... could you indulge us?

伴我心暖 2024-11-16 10:57:14

不要使用提交按钮,使用 。您的表单正在发布并返回一个新页面,其中按钮处于默认设置。

您可以忘记 xml 样式的标记,它只是浪费字符。而且void是一个运算符,而不是一个函数,所以不需要在它后面跟上分组运算符()。无论如何,您不需要它,所需的操作属性可以是任何内容,在这种情况下“#”就足够了 - 如果未提交表单,它就不会去任何地方。

<form action="#" id="theTimer" name="theTimer">
    <input id="timerDisplay" type="text" name="theTime" readonly>
    <br>
    <input type="button" name="start" value="Start" onclick="Start()">
    <input type="button" name="stop" value="Stop" onclick="Stop()">
    <input type="button" name="pause" value="Pause" onclick="Pause()">
</form>

按照约定,以大写字母开头的标识符保留给构造函数,而所有大写字母开头的标识符则保留给常量。

Don't use submit buttons, use <input type="button" ...>. Your form is posting and returning a new page with the buttons in their default settings.

You can forget the xml-style markup, it's just wasted characters. And void is an operator, not a function, so you don't need to follow it with the grouping operator (). You don't need it anyway, the required action attribute can be anything, '#' is sufficient in this case - if the form isn't submitted, it won't go anywhere.

<form action="#" id="theTimer" name="theTimer">
    <input id="timerDisplay" type="text" name="theTime" readonly>
    <br>
    <input type="button" name="start" value="Start" onclick="Start()">
    <input type="button" name="stop" value="Stop" onclick="Stop()">
    <input type="button" name="pause" value="Pause" onclick="Pause()">
</form>

By convention, identifiers starting with a capital letter are reserved for constructors and identifiers with all captial letters for constants.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文