简体   繁体   中英

html input type button submit on enter

Does anyone know how to make submit button on enter in my case?

<div data-bind="with: idea">
    <input type="text" data-bind='value:itemToAdd, valueUpdate: "afterkeydown"' />
    <input type="button" id="addButton" data-bind="click:$parent.addItem, enable: itemToAdd" value="add" />
    <ul data-bind="foreach:allItems">
        <li> <span data-bind="text:$data"></span>
                <input type="button" data-bind="click:$root.removeItem.bind($parent, $data)" value="-" />
        </li>
    </ul>
</div>

Here's fiddle

I would recommend to implement it this way:

<form data-bind="with: idea, submit: addItem">
    <input type="text" data-bind='value:itemToAdd, valueUpdate: "afterkeydown"' />
    <input type="submit" id="addButton" data-bind="enable: itemToAdd" value="add" />
    <ul data-bind="foreach:allItems">
        <li> <span data-bind="text:$data"></span>
                <input type="button" data-bind="click:$root.removeItem.bind($parent, $data)" value="-" />
        </li>
    </ul>
</div>

The idea is following: you're wrapping your controls with the form and adding submit binding to it, that means action, that will be performed on form submit. And now on pressing Enter inside the form you are actually submitting the from, that's why submit action will be performed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM