简体   繁体   English

Javascript如何用事件监听器移动变量?

[英]Javascript how to move variables with event-listener?

I have the following function that writes a multi-choice question from JSON object. 我有以下函数,可从JSON对象写入多项选择题。

First, is the event-listener creation here "legal"? 首先,这里的事件侦听器创建是否“合法”? And if so, I want the function executed by the listener (validate) to get the number of the form on which "submit" was clicked, and the number of the check-box that was checked. 如果是这样,我希望由侦听器执行的函数(验证)获取单击了“提交”的表单的编号,以及选中的复选框的编号。 How can I do that? 我怎样才能做到这一点? thanx. 谢谢。

function writeQuestions() {

len = questions.qestion.length;
for (var i = 0; i < len; i++) {

        answerRdy = [];
        qestionRdy = questions.qestion[i];
        answerRdy[0] = questions.answer[i][0];
        answerRdy[1] = questions.answer[i][1];
        answerRdy[2] = questions.answer[i][2];
        answerRdy[3] = questions.answer[i][3];
        divID = "question-" + i;
        formID = "form-" + i;
        CurrentForm = i;

            writeAnswer = [];
            writeAnswer[writeAnswer.length] = ("\n<div id='{0}'>\n<form id='{1}' name='{1}'>\n").format(divID, formID);
            writeAnswer[writeAnswer.length] = ("<b>" + qestionRdy + "</b><br />\n");
        for (n=0; n<=3; n++) {
            writeAnswer[writeAnswer.length] = ("<input type='radio' name='answerTo{0}' value='{1}' /> {2} <br />\n").format(CurrentForm, n, answerRdy[n]);
                }
            writeAnswer[writeAnswer.length] = ("<input type='submit' value='Submit your answer'>\n</form>\n</div><!--{0}-->").format(divID);


        joinQuestion = writeAnswer.join();
            exp = /,/gi;
        fullQuestion = joinQuestion.replace(exp, "");

    $('#container').append(fullQuestion);
    document.forms[i].addEventListener('submit',validate);
}
}

You are probably better off attaching the submit event using jquery submit() so the function would become a part of the form submit event handler 您最好使用jquery commit()附加Submit事件,这样该函数将成为表单Submit事件处理程序的一部分。

<script type="text/javascript">
$(document).ready(function(){
    $("form").submit(function(){
    alert($(this).attr("id"));
    alert("Checked radio " + $(this).find("input:checked").val())
    //return false if you do not want to submit the form, otherwise comment it out
    return false;
    });
    });

</script>

<form id="{1}">
<input type="radio" name="answer" value=1>
<input type="radio" name="answer" value=2>
<input type="submit" value="submit">
</form>
<form id="{2}">
<input type="radio" name="answer" value=3>
<input type="radio" name="answer" value=4>
<input type="submit" value="submit">
</form>

you should listen on click of submit button and form onclick should return false. 您应该监听提交按钮的点击,表单onclick应该返回false。 Use frameworks like jquery for making these type of functionality. 使用诸如jquery之类的框架来实现这些类型的功能。

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

相关问题 在JavaScript中触发单个事件监听器的多个动作(单击) - Triggering multiple actions for single event-listener(on-click) in javascript 如果调用Javascript事件侦听器并且缺少目标元素,会发生什么情况? - What happens if a Javascript event-listener is called and target element is missing? 有没有办法在 Javascript 中获取所有事件侦听器绑定? - Is there a way to get all event-listener bindings in Javascript? Javascript(Dojo):销毁对象时是否删除事件监听器 - Javascript (Dojo): Is event-listener removed when object is destroyed 如何从事件侦听器中排除向下箭头键 - How to exclude down arrow key from event-listener 按钮事件侦听器无响应 - Button Event-listener is unresponsive 关于问答游戏中的事件监听器 - About an event-listener inside a quiz game 我将如何将事件侦听器分配给一个 div 以在单击时允许 div 的宽度和高度增长 20% - How would I assign an event-listener to a div that allows the width and height of the div to grow 20% when clicked iOS Safari:“ mousemove”上有事件监听器时不会触发“ click”事件 - iOS Safari: 'click' event not triggerd when there is an event-listener on 'mousemove' Next.js:将“样式”添加到事件侦听器中的元素 - Next.js: Add 'style' to element in event-listener
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM