javascript 确定表单上是否存在字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3568591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 01:32:35  来源:igfitidea点击:

determining if a field exists on a form

javascript

提问by EmmyS

I have a form field (a series of checkboxes) that's being created dynamically from a database, so it's possible that the field will not exist on the form (if there are no matching values in the database). I have some code that needs to execute based on whether the field exists, and pull in the values that are selected if it does exist. I can't seem to get javascript to acknowledge that this field exists, though. Here's what I've tried:

我有一个从数据库动态创建的表单字段(一系列复选框),因此表单上可能不存在该字段(如果数据库中没有匹配的值)。我有一些代码需要根据字段是否存在来执行,并在它存在时拉入选择的值。不过,我似乎无法让 javascript 承认该字段存在。这是我尝试过的:

function displayAction(){
    var f = document.adminForm;
    var a = f.action;

    if(f.prefix.value!="-") {
        a = a + '&task=callExclusionDisplay&prefix=' + f.prefix.value;
    }
    else {
        var exclusions = document.getElementById("exclusions");
        if (exclusions != null){
            alert("exclusions set");
            a = a + '&task=callExclusionCreate&prefix=' + f.prefix.value + '&exclusions=' + exclusions.join();
        }
    }
    alert('after if, action is ' + a);
}

The code never passes the if statement checking to see if exclusions is not null, even though when I look at the page there are a number of checkboxes named exclusions (with the id also set to exclusions). Is the issue with !=null because it's a group of checkboxes, rather than a single form element? How can I get this to work? If I skip the test for null, the code throws errors about exclusions not being defined if the database doesn't return any matching values.

代码从不通过 if 语句检查以查看排除项是否不为空,即使当我查看页面时有许多名为排除项的复选框(ID 也设置为排除项)。!=null 的问题是因为它是一组复选框,而不是单个表单元素吗?我怎样才能让它工作?如果我跳过 null 测试,如果数据库没有返回任何匹配的值,代码会抛出关于未定义排除项的错误。

采纳答案by Lekensteyn

You're using document.getElementById, but form elements have a name. Try f.elements.namedItem("exclusions")instead of exclusions != null

您正在使用document.getElementById,但表单元素有一个名称。尝试f.elements.namedItem("exclusions")代替exclusions != null

回答by Daniel Vandersluis

Multiple elements in the same page cannot share an idattribute (ie. idmust be unique or unset). As well, though some (older) browsers erroneously collect elements whose name matches the ID being looked for with getElementById, this is invalid and will not work cross-browser.

同一页面中的多个元素不能共享一个id属性(即id必须是唯一的或未设置的)。同样,尽管一些(较旧的)浏览器错误地收集名称与正在查找的 ID 匹配的元素getElementById,但这是无效的,并且不能跨浏览器工作。

If you want to get a group of elements, you can give them all the same nameattribute, and use document.getElementsByNameto get the group. Note that the result of that will be a NodeListwhich is kind of like an array in that it can be iterated over.

如果你想获取一组元素,你可以给它们所有相同的name属性,并使用document.getElementsByName来获取组。请注意,其结果将是一个NodeList类似于数组的 a ,因为它可以迭代。

回答by 7wp

if you have more than one element with the id "exclusions" it will screw up the functionality of getElementById. I would remove the duplicate "exclusions" ids from all of your elements and use getElementByName() instead, and give your group of checkboxes the name="exclusions" instead.

如果您有多个元素的 id 为“exclusions”,它将破坏 getElementById 的功能。我会从您的所有元素中删除重复的“排除”ID,并使用 getElementByName() 代替,并为您的复选框组指定 name="exclusions" 。

Edit: But there is a much simpler way using jQuery, and it gives you some cross browser compability guarrantee. To do the same thing with jQuerydo this:

编辑:但是使用jQuery有一种更简单的方法,它为您提供了一些跨浏览器兼容性保证。要使用jQuery做同样的事情,请执行以下操作:

var checkBoxesExist = $('[name=exclusions]').count() > 0;

Or if you have given your elements unique ID's then you can do this:

或者,如果您为元素指定了唯一 ID,则可以执行以下操作:

var checkbox1exists = $('#checkBox1').count() > 0;

回答by Dutchie432

Each element must have a unique ID.

每个元素都必须有一个唯一的 ID。

Then, you can check just like this:

然后,您可以像这样检查:

if (document.getElementById('exclusions1')) {
    //field exists
}

Or if you need to loop through a bunch of them:

或者,如果您需要遍历其中的一堆:

for (x=0; x<10; x++) {
    if (document.getElementById('exclusions' + x)) {
        //field X exists
    }
}

回答by Rahul

Do all the checkboxes have the same id == exclusions? If yes, then you must first correct that.

所有复选框都具有相同的 id == 排除项吗?如果是,那么您必须首先纠正它。

Before you do so, did you try checking the first checkbox and see if the if condition goes through?

在您这样做之前,您是否尝试检查第一个复选框并查看 if 条件是否通过?