gpt4 book ai didi

Javascript 名称数组输入选择器 (name[sometext])

转载 作者:行者123 更新时间:2023-12-03 21:43:55 25 4
gpt4 key购买 nike

如何在 jQuery 中正确选择这样的命名输入而不分配 ID?

<input name="person[first]" type="hidden">
<input name="person[last]" type="hidden">

// Seems to be acting on multiple hidden elements instead of this named one
// This sets the value of both of those hidden elements in some cases
$("input[type=hidden][name=person[first]]").val('Test');

// So I've changed it to this. Is there a better way?
$("input[name=person[first]]").val('Test');

最佳答案

将属性值放在引号中...

$("input[type=hidden][name='person[first]']").val();
// ---------------------^-------------^
<小时/>

来自 the docs for attribute-equals-selector ...

attribute An attribute name.

value An attribute value. Can be either an unquoted single word or a quoted string.

由于您有多个单词,因此应该将其引用。

关于Javascript 名称数组输入选择器 (name[sometext]),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9252217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com