Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery是否;更改“;是否处理从外部文件加载的字段?_Javascript_Jquery_Forms_External_Onchange - Fatal编程技术网

Javascript jQuery是否;更改“;是否处理从外部文件加载的字段?

Javascript jQuery是否;更改“;是否处理从外部文件加载的字段?,javascript,jquery,forms,external,onchange,Javascript,Jquery,Forms,External,Onchange,我有三种不同的表单,通过一个外部的.html文件插入。表单一次显示一个。当用户按下“ok”或“back”时,表单将更改并加载其他两个表单中的一个。到目前为止,这一切正常 现在我想跟踪用户的输入,并考虑使用jQuery“change”分别为每个输入字段触发,但它不起作用。任何帮助都会很好 var tabContent = new XMLHttpRequest(); tabContent.onreadystatechange = processText; tabContent.open("GET",

我有三种不同的表单,通过一个外部的.html文件插入。表单一次显示一个。当用户按下“ok”或“back”时,表单将更改并加载其他两个表单中的一个。到目前为止,这一切正常

现在我想跟踪用户的输入,并考虑使用jQuery“change”分别为每个输入字段触发,但它不起作用。任何帮助都会很好

var tabContent = new XMLHttpRequest();
tabContent.onreadystatechange = processText;
tabContent.open("GET", "tabd.html", true);
tabContent.send();

function processText() {
    if (tabContent.readyState == 4) {
    tab = tabContent.responseText;
    document.getElementById("tabholder").innerHTML += tab;
    focusOnElem();
    }
}
表单字段:

<input id="day" value="15" maxlength="2"/>

每次从dom中删除元素时,它都将丢失其关联的绑定。你需要重新绑定,把它放到一个函数中

每次在页面中重新插入day时,调用Rebind()


绑定事件时,触发该事件的元素必须存在才能绑定该事件。将事件处理程序附加到页面加载时的一组元素时,只有DOM中当前存在的元素受该事件处理程序的影响。要包含DOM中不存在但稍后将动态插入的元素,必须将事件委托给DOM中实际存在的更高级别的元素,并检查目标元素是否与选择器匹配。这称为委托事件处理程序,在jQuery中如下所示:

$('#tabholder').on('change', '#day', function(){
    console.log("change");
});

谢谢你的帮助!我试过你的建议,但没用。我注意到

$('#tabholder').on('change', '#day', function(){
    console.log("change");
});
只适用于文本区域,不适用于任何其他输入。这是否与我正在更改它们的值而不是innerHTML有关

不管怎么说,对我有效的方法是使用两个函数,一个用于存储当前值,每当iser按下一个键时就会调用:

document.onkeydown = function(k) {
        memorise();
}
function memorise() {
        console.log("memorise");
        if (stage == 1) {
            dayMemo = $("#day").attr("value");
            monthMemo = $("#month").attr("value");
            console.log(dayMemo, monthMemo);
        } else if (stage == 2) {
            hourMemo = $("#hour").attr("value");
            minuteMemo = $("#minute").attr("value");
            console.log(hourMemo, minuteMemo);
        } else if (stage == 3) {
            commentMemo = $("#comment").attr("value");
            console.log(commentMemo);
        }
    }
为了恢复这些值,每当我从外部文件加载某些内容时,就会调用这个值:

var tabContent = new XMLHttpRequest();
tabContent.onreadystatechange = processText;
tabContent.open("GET", "tabd.html", true);
tabContent.send();

//this function gets external file and shows previous input values
function processText() {
            if (tabContent.readyState == 4) {
                tab = tabContent.responseText;
                document.getElementById("tabholder").innerHTML += tab;
                focusOnElem();
                showMemo();
            }
        }
function showMemo() {
        $("#day").attr('value', dayMemo);
        $("#month").attr('value', monthMemo);
        $("#hour").attr('value', hourMemo);
        $("#minute").attr('value', minuteMemo);
        $("#comment").attr('value', commentMemo);
    }

…或者他们可以简单地使用委托形式
.on()
,这是为此类情况量身定制的。仅供参考:“从jQuery 1.7开始,不建议使用.die()(及其补充方法.live())。而是使用.off()删除与.on()绑定的事件处理程序。”
document.onkeydown = function(k) {
        memorise();
}
function memorise() {
        console.log("memorise");
        if (stage == 1) {
            dayMemo = $("#day").attr("value");
            monthMemo = $("#month").attr("value");
            console.log(dayMemo, monthMemo);
        } else if (stage == 2) {
            hourMemo = $("#hour").attr("value");
            minuteMemo = $("#minute").attr("value");
            console.log(hourMemo, minuteMemo);
        } else if (stage == 3) {
            commentMemo = $("#comment").attr("value");
            console.log(commentMemo);
        }
    }
var tabContent = new XMLHttpRequest();
tabContent.onreadystatechange = processText;
tabContent.open("GET", "tabd.html", true);
tabContent.send();

//this function gets external file and shows previous input values
function processText() {
            if (tabContent.readyState == 4) {
                tab = tabContent.responseText;
                document.getElementById("tabholder").innerHTML += tab;
                focusOnElem();
                showMemo();
            }
        }
function showMemo() {
        $("#day").attr('value', dayMemo);
        $("#month").attr('value', monthMemo);
        $("#hour").attr('value', hourMemo);
        $("#minute").attr('value', minuteMemo);
        $("#comment").attr('value', commentMemo);
    }