简体   繁体   中英

jQuery: Getting value from input array

We have the following form:

<form>
...
      <table width="100%" cellspacing="0" class="datagrid_ppal">
        <tbody>
          <tr>
            <th scope="row">Area 1 <input name="config_line" type="hidden" value="0,5,50" /></th>
            <td class="gantt"> </td>
            <td class="gantt"> </td>
            <td class="gantt"> </td>
            ...
          </tr>
        </tbody>
      </table width="100%" cellspacing="0" class="datagrid_ppal">
...
<form>

What we need is to get the first, second or third from the hidden input value. We have tried this and didn't work:

        var value = $('th').children('input:hidden').val();

Can anyone help us? We would really appreciate it.

The value of your hidden field is not an array, but just the string: "0,5,50".

To retrieve that value using jQuery use:

$('input[name=config_line]').val()

To split that string into an array use the split() method.

Combined:

var firstValue = $('input[name=config_line]').val().split(",")[0]; // etc...
var value = $('input[name=config_line]').val();
var valueArry = value.split(',');
var v1 = valueArry[0];
var v2 = valueArry[1];
var v3 = valueArry[2];

You'll find details Here .

var value = $('th').children("input[type='hidden']").val();

should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM