Javascript jQuery - 将数组值设置为具有相同名称的输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46195479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:21:24  来源:igfitidea点击:

jQuery - set array values to the input with the same name

javascriptphpjqueryarrays

提问by Keyur

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

我试图将数组值设置为名为的输入字段 attachments[]

I have array stored in js variable attachArray

我将数组存储在 js 变量中 attachArray

What I have tried is,

我尝试过的是,

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

or

或者

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

But getting empty attachmentsin the controller like this,

但是attachments像这样在控制器中变空,

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>

回答by lumio

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.

据我了解,您希望将 JavaScript 数组的内容传播到 PHP 将其解释为数组的多个字段。

I changed the inputs from hiddento textjust 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.

我将输入从hidden改为text只是为了让它更清晰一点,这样您就可以看到值的样子。不要忘记在您的代码中撤消此操作。

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_decodeto convert it back into an array, like so:

但这是很多代码要做。我认为将数组作为 JSON 发送并使用 PHPjson_decode将其转换回数组可能更容易,如下所示:

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' ] );

回答by Anant Singh---Alive to Die

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)).

注意:- 使用反斜杠(使用\\(无空格)转义内部括号)。

回答by Arun Kumaresh

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

你有名字的输入字段,attachments[]所以试试这个

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