简体   繁体   English

有没有一种方法可以防止用户在至少移动滑块(输入类型=范围)之前提交表单?

[英]Is there a way to prevent the user from submitting form before s/he has at least moved the slider (input type = range)?

I have a slider in my html form like this. 我的html表单中有一个这样的滑块。 It is for an online experiment. 这是一个在线实验。 I have already set it to be required, but the thing is I wanna make sure the user has manipulated the slider in any way before submitting (ie, click it at least). 我已经设置了它为必填项,但是我想确保用户在提交之前已经以任何方式操纵了滑块(即,至少单击了它)。 Because the slider comes at 50% position chose already by default, the user could just click "OK" without actually thinking about the answer. 由于滑块默认处于已选择的50%位置,因此用户可以单击“确定”而无需实际考虑答案。 In this way, I could behaviorally force them to click it at least and make some reflections. 这样,我可以在行为上强迫他们至少单击它并进行一些思考。

I've done Googling and didn't find any solutions on this. 我完成了谷歌搜索,没有找到任何解决方案。

<form id="q-familiarity">
  <label for="fam slider">How well are you familiar with this item ?   </label><br>
  <input name="fam slider" type="range" min="1" max="100" value="50"  step="0.5" class="slider" id="fam-slider" required>
  <button type="submit" form="q-familiarity">OK</button>
<form>

How could I implement a mechanism, where user has to at least click the slider once to "activate" the OK button (perhaps also OK goes from grey to black to indicate the change)? 我该如何实现一种机制,即用户至少必须单击一次滑块才能“激活”“确定”按钮(也许“确定”也从灰色变为黑色以指示更改)?

I would prefer the solution to be html and css based, with as little js as possible. 我希望解决方案基于html和css,并尽可能少使用js。 (I learned html and css by myself for the experiment and don't know any js) But I would guess js has to be used in the solution, which is ok. (我自己为实验学习了html和css,但不知道任何js。)但是我猜想在解决方案中必须使用js,这是可以的。

Thank you. 谢谢。

I'd suggest to disable the submit button by default and enable only if the user interacts with the input, like so 我建议默认情况下禁用提交按钮,并且仅在用户与输入进行交互时才启用,例如

Codepen demo Codepen示范

<form>
  <label for="fam slider">How well are you familiar with this item ? </label><br>
  <input type="range" min="1" max="100" value="50" step="0.5"  id="fam-slider" required>
  <button type="submit" id="submit-btt" disabled="disabled">OK</button>
<form>

Js s

let slider = document.getElementById('fam-slider');
let button = document.getElementById('submit-btt');
slider.addEventListener('input', () => {
  button.removeAttribute('disabled');
})

At the input event on the slider remove the disabled attribute on the submit button. 在滑块上的input事件中,删除Submit按钮上的disabled属性。

I've used the input event instead of the change event because it is triggered as soon as the user drags the slider control, and allows the user to also choose the starting value (if he wants to) 我使用了input事件而不是change事件,因为它是在用户拖动滑块控件时立即触发的,并允许用户还选择起始值(如果他愿意)

Well, unfortunately you need to use JS anyway to do it. 好吧,不幸的是,您仍然需要使用JS来完成它。 If I have understood correctly, you're looking for a simple change event . 如果我理解正确,那么您正在寻找一个简单的change事件

 var slider = document.getElementById( 'fam-slider' ); var submit = document.getElementById( 'submit' ); slider.addEventListener( 'change', function() { submit.disabled = false; }); 
 <form id="q-familiarity"> <label for="fam slider">How well are you familiar with this item ? </label><br> <input name="fam slider" type="range" min="1" max="100" value="50" step="0.5" class="slider" id="fam-slider" required> <button id="submit" type="submit" form="q-familiarity" disabled>OK</button> <form> 

You can check if the slider was changed or clicked and set a validation variable. 您可以检查滑块是否被更改或单击并设置验证变量。 Then on submit check your "valid" flag: 然后在提交时检查您的“有效”标志:

 var valid = false; $("input.slider").on("change click", function(){ valid = true; }); $("#submit").click(function(){ if (!valid){ // do your logic here console.log("not valid"); return false; } }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form id="q-familiarity"> <label for="fam slider">How well are you familiar with this item ? </label><br> <input name="fam slider" type="range" min="1" max="100" value="50" step="0.5" class="slider" id="fam-slider" required> <button id="submit" type="submit" form="q-familiarity">OK</button> <form> 

Here is a jQuery solution for you if you prefer it over vanilla JS. 如果您喜欢香草JS,则可以使用这里的jQuery解决方案。 The button is by default disabled, but as soon as you change the slider it becomes active. 默认情况下,该按钮处于禁用状态,但是一旦您更改滑块,它就会变为活动状态。

 $('#slider').change(function() { $("#submitbutton").prop("disabled", false); }) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form id="q-familiarity"> <label for="fam slider">How well are you familiar with this item ? </label><br> <input id='slider' name="fam slider" type="range" min="1" max="100" value="50" step="0.5" class="slider" id="fam-slider" required> <button id= "submitbutton" type="submit" disabled='true' form="q-familiarity">OK</button> <form> 

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

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