I have HTML like this:

<div class="icheckbox_flat-aero checked" style="position: relative;">
    <input type="checkbox" class="icheck" id="Checkbox9" name="userAccessNeeded" target="Checkbox6" style="position: absolute; opacity: 0;">
    <ins class="iCheck-helper" style="position: absolute; top: 0%; left: 0%; display: block; width: 100%; height: 100%; margin: 0px; padding: 0px; background-color: rgb(255, 255, 255); border: 0px; opacity: 0; background-position: initial initial; background-repeat: initial initial;"></ins>
</div>

This is my javascript:

$('.iCheck-helper').click(function () {  // the element I clicked on
    var parent = $(this).parent(); // gets the div element
    var classNames = parent.find($("input")).prop("target"); // gets the input elements target
    alert(classNames);
});

It should work, but it alert undefined. What am I doing wrong?

有帮助吗?

解决方案 3

this should fix it:

$('.iCheck-helper').click(function () {
    var parent = $(this).parent(); 
    var classNames = parent.find("input").attr("target");
    alert(classNames);
});

changed

parent.find($("input")).prop("target");

to

parent.find("input").attr("target");

Here's a demo: http://jsbin.com/evafub/1/edit

其他提示

This is because there is no such property named target in the DOM interface of the input element.

input elements don't have a target attribute, only a formtarget (and equivalently there's the formTarget property). I'm not sure what the purpose of your "target" is in your code, but a formTarget is a reference to an ID of a form:

The target and formtarget content attributes, if specified, must have values that are valid browsing context names or keywords.

The target of an element is the value of the element's formtarget attribute, if the element is a submit button and has such an attribute; or the value of its form owner's target attribute, if it has such an attribute; or, if the Document contains a base element with a target attribute, then the value of the target attribute of the first such base element; or, if there is no such element, the empty string.

If your "target" is for another purpose, use a data-* attribute to store the value (i.e.data-target="Checkbox6").

You need to use .attr() here because, target is an attribute here

var classNames = parent.find("input").attr("target");

or

var classNames = $(this).prev().attr("target");
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top