使用内联javascript从按钮onclick设置文本框的值

时间:2017-11-30 02:32:17

标签: javascript jquery html

使用内联javascript设置onClick按钮上的文本框值

我有一个文本框和一些按钮,其中包含一个在值和data-for-count中定义的整数。 我知道如何在外部.js文件中使用jquery添加计数值,但是可以在不调用函数的情况下执行此操作吗?

HTML

<div class="wrap">
    <div class="thediv">
        <input type="number" class="count" value="10" min="10" max="100" />
    </div>
    <div>
        <input type="button" class="quick" value="10" data-for-count="10" />
        <input type="button" class="quick" value="20" data-for-count="20" />
        <input type="button" class="quick" value="30" data-for-count="30" />
    </div>
</div>

这是我到目前为止所尝试的:

<input type="button" class="quick" value="77" data-for-count="77" onclick="this.closest('wrap').find('input.count').val(this.data('id'))" />

它失败了,因为它的jquery而不是纯粹的javascript,但它显示了我想要做的事情

1 个答案:

答案 0 :(得分:0)

<input type="button" class="quick" value="10" data-for-count="10" onclick="document.querySelector('.count').value = this.dataset['forCount'];" />