最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - How to get index of input in javascript - can use jQuery - Stack Overflow

programmeradmin5浏览0评论

I have an array of inputs generated from js code. I have set the name of the inputs like this: name="myTextInput[]"

How can I get the index of the selected input?

I tried something like:

 onClick="oc(this);" 

where:

function oc(inp)
{
    return(inp.index);
}

but is not working.

I can use jQuery as well

I have an array of inputs generated from js code. I have set the name of the inputs like this: name="myTextInput[]"

How can I get the index of the selected input?

I tried something like:

 onClick="oc(this);" 

where:

function oc(inp)
{
    return(inp.index);
}

but is not working.

I can use jQuery as well

Share Improve this question edited Mar 2, 2012 at 13:55 Blazemonger 93.1k27 gold badges145 silver badges181 bronze badges asked Mar 2, 2012 at 13:30 ErvinErvin 2,4524 gold badges32 silver badges46 bronze badges 2
  • 1 You're not using jQuery in your sample code. Do you want to? – Blazemonger Commented Mar 2, 2012 at 13:32
  • In the future, please try to provide a plete code sample so that we understand exactly what you're working on and what you're trying to solve. – Blazemonger Commented Mar 2, 2012 at 13:55
Add a ment  | 

4 Answers 4

Reset to default 3

You can use the EACH function in jquery. This will parse through the set of matched elements. You can put a custom function inside that will use the index of each element, as you parse through, as an argument.

$('input').each(function(index){
    alert(index);
});

You can also get the value of each input like this:

$('input').each(function(index, val){
    alert(index + ' has value: ' + val);
});

see details here: http://api.jquery./jQuery.each/

** EDIT **

If you want the value shown in an alert box on click, use the each function and the click function together. Remember to get the real-time value of the input, use $(this).val(). Return index and value data on click:

$('input').each(function(index, val){
    $(this).click(function(){
        alert(index + ' has value: ' + $(this).val());
    });
});

You could get the input like this (not sure if you actually wanted the click event though)...

var inputs = $('input[name="myTextInput[]"]');

inputs.click(function() {
    alert(inputs.index(this));
});

Please use the index() method to find the position of an element.

Check out this example: http://jsbin./uyucuv/edit#javascript,html

<ul>
  <li id="foo">foo</li>
  <li id="bar">bar</li>
  <li id="baz">baz</li>
</ul>


$(function() {
  $("li").on("click", function() {
    alert($(this).index());
  });
});

Check the index() documentation here: http://api.jquery./index/

Hope this helps!

The "jQuery way" is to avoid onClick="whatever()" and use pure JavaScript separate from the HTML tags. Try this between a pair of <script> tags (note: requires jQuery 1.7 or higher):

$('input').on('click', function() {
    var varname = $(this).attr('name'),
        $arr = $('input[name="'+varname+'"]'),
        idx = $arr.index(this);

    alert(idx);
});​

http://jsfiddle/mblase75/EK4xC/

发布评论

评论列表(0)

  1. 暂无评论