我正在 jQuery 控制台中寻找一种方法来在 Chrome 中选中选中的值(即 416800-000)。我找到了方法,但我的 HTML 情况有点不同。关闭 name 参数对我来说是不可行的。
<input type="hidden" name="value[1].value" value="416800-000" class="__web-inspector-hide-shortcut__" checked="checked">
<input type="checkbox" name="value[1].enabled" value="on">
当类型被隐藏时上面的值可以被动态的。
这是我的问题的演示
https://jsfiddle.net/68ujpfrw/1/
类似本案例Get checkbox with specific value
编辑:另外,是否可以找到文本(即管理员)然后选中复选框?
<input type="hidden" name="value[1].value" value="416800-000" class="__web-inspector-hide-shortcut__" checked="checked">
<input type="checkbox" name="value[1].enabled" value="on">
"Admin"
最佳答案
好吧,因为实际/可见的复选框在隐藏的复选框之后,所以您可以简单地使用 .next()
然后设置属性值。
$("input[value='416800-000']").next().prop("checked", true);
更新了 JSFiddle
$("input[value='416800-000']").next().prop('checked', true);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="hidden" name="value[1].value" value="416800-000" class="__web-inspector-hide-shortcut__" checked="checked">
<input type="checkbox" name="value[1].enabled" value="on">
编辑:我建议在 span
标签中添加“Admin”文本以帮助选择元素,然后使用 .prev()
选中复选框
$("span:contains('Admin')").prev().prop('checked', true);
更新了 Fiddle对于这种情况。
$("span:contains('Super User')").prev().prop('checked', true);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="hidden" name="value[1].value" value="416800-000" class="__web-inspector-hide-shortcut__" checked="checked">
<input type="checkbox" name="value[1].enabled" value="on">
<span>Admin</span>
<input type="hidden" name="value[1].value" value="416900-000" class="__web-inspector-hide-shortcut__" checked="checked">
<input type="checkbox" name="value[1].enabled" value="on">
<span>User</span>
<input type="hidden" name="value[1].value" value="417000-000" class="__web-inspector-hide-shortcut__" checked="checked">
<input type="checkbox" name="value[1].enabled" value="on">
<span>Super User</span>
关于javascript - 设置具有特定值的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34760122/