简体   繁体   English

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

[英]setting value of textbox from button onclick using inline javascript

I have a textbox and some buttons with an integer defined in value and data-for-count attributes.我有一个文本框和一些按钮,在valuedata-for-count属性中定义了一个整数。 I know how to add the count value using jQuery in an external .js file, but is it possible to do this without calling a function?我知道如何在外部 .js 文件中使用 jQuery 添加计数值,但是否可以在不调用函数的情况下执行此操作?

HTML : 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>

This is what I've tried so far:这是我迄今为止尝试过的:

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

It fails since it's jQuery and not pure javascript, but it displays what I want to do.它失败了,因为它是 jQuery 而不是纯 javascript,但它显示了我想要做的事情。

 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <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> <input type="button" class="quick" value="77" data-for-count="77" onclick="$(this).closest('.wrap').find('input.count').val($(this).data('for-count'))" /> </div>

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

 <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="10" data-for-count="10" onclick="document.querySelector('.count').value = this.dataset['forCount'];" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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