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

javascript - jQuery - set array values to the input with the same name - Stack Overflow

programmeradmin4浏览0评论

I was trying to set the array values to the input field named attachments[]

I have array stored in js variable attachArray

What I have tried is,

$('[name="attachments"]').attr('value', attachArray);

or

$('[name="attachments"]').val(attachArray);

But getting empty attachments in the controller like this,

array(1) { ["attachments"]=> array(1) { [0]=> string(0) "" } }

What I'm doing wrong?

EDIT

<div class="col-md-4">
    <div class="form-group ticket-align">
        <label>Attachment</label>
        <label class="btn btn-primary" data-toggle="modal" data-target="#t-attachment-modal">
                    Browse&hellip; 
             <input type="hidden" name="attachments[]">
         </label>
         <span id="fileList"></span>
         <span class="error" id="error-atachments" style='display: none;'></span>
    </div>
</div>

I was trying to set the array values to the input field named attachments[]

I have array stored in js variable attachArray

What I have tried is,

$('[name="attachments"]').attr('value', attachArray);

or

$('[name="attachments"]').val(attachArray);

But getting empty attachments in the controller like this,

array(1) { ["attachments"]=> array(1) { [0]=> string(0) "" } }

What I'm doing wrong?

EDIT

<div class="col-md-4">
    <div class="form-group ticket-align">
        <label>Attachment</label>
        <label class="btn btn-primary" data-toggle="modal" data-target="#t-attachment-modal">
                    Browse&hellip; 
             <input type="hidden" name="attachments[]">
         </label>
         <span id="fileList"></span>
         <span class="error" id="error-atachments" style='display: none;'></span>
    </div>
</div>
Share Improve this question edited Sep 13, 2017 at 10:52 Keyur asked Sep 13, 2017 at 10:46 KeyurKeyur 1,1113 gold badges23 silver badges42 bronze badges 4
  • @AlivetoDie Didn't succeed. – Keyur Commented Sep 13, 2017 at 10:49
  • This is an array of what? – Stuart Commented Sep 13, 2017 at 10:51
  • @Stuart Its an array of strings – Keyur Commented Sep 13, 2017 at 10:53
  • 2 So $('[name=attachments\\[\\]]').val(attachArray); OR $('input:hidden[name=attachments\\[\\]]').val(attachArray); will work – Death-is-the-real-truth Commented Sep 13, 2017 at 10:54
Add a comment  | 

4 Answers 4

Reset to default 7

As far as I understand is that you want to spread the content of an JavaScript array to multiple fields that PHP on the other hand interprets as an array.

I changed the inputs from hidden to text just to make it a little bit more clear and so you can see how the values do look like. Don't forget to undo this in your code.

const attachArray = [
  'val1',
  'val2',
];
const attachments = $('[name="attachments[]"]');
for ( let i = 0; i < attachments.length; i += 1 ) {
  $( attachments[ i ] ).val( attachArray[ i ] );
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-md-4">
    <div class="form-group ticket-align">
        <label>Attachment</label>
        <label class="btn btn-primary" data-toggle="modal" data-target="#t-attachment-modal">
                    Browse&hellip; <br>
             <input type="text" name="attachments[]"><br>
             <input type="text" name="attachments[]">
         </label>
         <span id="fileList"></span>
         <span class="error" id="error-atachments" style='display: none;'></span>
    </div>
</div>

But this is a lot of code to do. I think it might be easier to send the array as JSON and use PHPs json_decode to convert it back into an array, like so:

const attachArray = [
  'val1',
  'val2',
];
$('[name="attachments"]').val( JSON.stringify( attachArray ) );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-md-4">
    <div class="form-group ticket-align">
        <label>Attachment</label>
        <label class="btn btn-primary" data-toggle="modal" data-target="#t-attachment-modal">
                    Browse&hellip; <br>
             <input type="text" name="attachments">
         </label>
         <span id="fileList"></span>
         <span class="error" id="error-atachments" style='display: none;'></span>
    </div>
</div>

And do something like

$attachments = json_decode( $_POST[ 'attachments' ] );

Since it's a hidden input, so

Either:-

$('[name=attachments]').val(attachArray); 

OR

$('input:hidden[name=attachments]').val(attachArray);

Will work.

Note:- Use the backslashes(Escape internal brackets with \\(no space)).

you have input field with name as attachments[] so try this

$('[name="attachments[]"]').val(attachArray);

Mention id to a hidden field and you are sending array through the form so declare hidden field name with square brackets like below.

<input type="text" name="attachments[]" id="attachments">

And bind your array with the jquery val() method.

$('#attachments').val(attachArray);

For Future Reference :-)

发布评论

评论列表(0)

  1. 暂无评论