Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
230 views
in Technique[技术] by (71.8m points)

php - Grab the name of an element with jquery

I have a series of Form Elements each with different names, I'll post one as an example. I cannot hard code the name into Jquery because unless I inspect the element, I won't know the name.

With that aside heres the element:

<label class="checkbox">
    <input type="checkbox" 
        name="aisis_options[package_Aisis-Related-Posts-Package-master]"
        value="package_Aisis-Related-Posts-Package-master" checked="" /> 
    Aisis-Related-Posts-Package-master 
    <a href="#">(Disable)</a>
</label>

The catch is to do this:

Grab the name of this element - upon clicking disable - and do two things, one - if the element is checked, which in this case it's not, unchecked it, two pass the name to a php variable, which then can do processing.

How would I do this? Jquery is not my strong area.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is a example without knowing more of your code:

$(function () {
    $('input:checkbox').click(function () {
        $(this).prop('disabled', true);
        var iName = this.name;
        $.ajax({
            url: "file.php",
            data: {
                'inputname': iName
            },
            success: function (data) {
                alert(data.returned_val);
            }
        })
    })
})

Demo here

If you want to reach the input via name directly you need to use double backslasshes to escape the square brackets and reach that input via name. Use:

$('input[name=aisis_options\[package_Aisis-Related-Posts-Package-master\]]')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...