我有一个动态生成的输入字段,通过单击添加按钮可以生成另一个输入字段。
<div class="entry input-group col-md-12">
<input class="form-control" name="fields[]" type="url" placeholder="Type URL" />
<span class="input-group-btn">
<button class="btn btn-success btn-add" type="button">
<span class="glyphicon glyphicon-plus"></span>
</button>
</span>
</div>
问题是我想通过 Ajax 获取这些输入字段的值,但只有第一个输入字段值被传递?
<script>
function launchAjax(){
$.post{"submit.php",{
url: $("[name='fields[]']").val(),
},
);
};
不知道如何修复它,如果我确实得到了这个修复,我也不知道如何在 PHP 中处理它。谢谢你的帮助
最佳答案
jQuery attribute selector必须遵守以下规则
- double quotes inside single quotes: $('a[rel="nofollow self"]')
- single quotes inside double quotes: $("a[rel='nofollow self']")
- escaped single quotes inside single quotes: $('a[rel=\'nofollow self\']')
- escaped double quotes inside double quotes: $("a[rel=\"nofollow self\"]")
在以前的 jQuery 版本中,选择器无需引号即可工作(更多详细信息 - https://github.com/jquery/jquery/issues/2824 )
解决您的问题(只需添加引号)
1) $("[name=\"fields[]\"]")
2) $('[name=\'fields[]\']')
3) $("[name='fields[]']");
4) $('[name="fields[]"]"');
关于javascript - 通过ajax将动态生成的输入字段的值传递给php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49818396/