英文:
How to use Checked HTML Attribute in Svelte Checkbox?
问题
我正在使用Laravel和Svelte制作一个简单的CRUD。我有一个复选框字段,并且想要通过给予 "checked" 属性来默认选中它。问题是,如果我尝试这样做,Svelte会抛出错误,说 "属性需要是唯一的":
```html
<input type="checkbox" class="form-check-input" id="remember" bind:checked={$form.remember} checked>
在这种情况下,如何使用HTML属性 "checked"?
<details>
<summary>英文:</summary>
I'm making a simple CRUD in Laravel and Svelte. I have one checkbox field and I would like to give "checked" attribute so it's checked by default. The problem is that Svelte throws error if I try to do so saying "Attribute needs to be unique":
<input type="checkbox" class="form-check-input" id="remember" bind:checked={$form.remember} checked>
How do I use checked HTML attribute in this case?
</details>
# 答案1
**得分**: 0
如果这是所期望的默认设置,我只需相应地初始化`form`存储。
另一种选择可能是使用一个动作,例如:
```js
function check(node) { node.setAttribute('checked', '') }
<input ... use:check />
不建议这样做,因为它与现有的绑定冲突。
英文:
If that is the desired default, I would just initialize the form
store accordingly.
An alternative might be using an action, e.g.
function check(node) { node.setAttribute('checked', '') }
<input ... use:check />
Would not recommend that, it fights the existing binding.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论