简体   繁体   English

如何创建一个按钮,单击后可改变外观?

[英]How to create a Button that changes visuals when clicked?

I want to create a Submit button that when clicked once will change it's design from "Submit" to an "I agree to the TOS" checkbox and only once that is checked the actual action/function will initiate. 我要创建一个“提交”按钮,单击该按钮将把其设计从“提交”更改为“我同意TOS”复选框,只有选中该按钮,实际操作/功能才会启动。

Does anybody have an elegant solution to this? 有人对此有一个优雅的解决方案吗?

Try this: 尝试这个:

html: 的HTML:

<form>
  <div id="TOC">
      <label for="TOC_check">I agree to TOC</label>
      <input type="checkbox" id="TOC_check"/>
  </div>
  <input type="submit"/>
</form>

css: CSS:

#TOC { display: none; }

js: js:

$(function() {
  $('form').submit(function() {
    if (!$('#TOC_check').is(':checked')) {
      $('#TOC').show();
      $('input[type="submit"]').hide();
      return false;
    }
  });
  $('#TOC_check').change(function() {
    if ($(this).is(':checked')) {
      $('input[type="submit"]').show();
    } else {
      $('input[type="submit"]').hide();
    }
  });
});

working JSFIDDLE 工作的JSFIDDLE

Assuming that they are the only siblings you can use .hide() and .show() like this: 假设他们是可以使用的唯一的兄弟姐妹.hide().show()是这样的:

 $('myclass').click(function(){//give them the same class name
      $(this).hide();
      $(this).siblings().show();
 })

Html example: HTML示例:

<form name="" action="" id="the_form">
    <input type="checkbox" value="some value" style="display: none;" class="check"/>
    <input type="submit" value="Submit" class="submit_button"/>
</form>

Script example: 脚本示例:

$(document).ready(function(){
    $(".submit_button").click(function(event){
         event.preventDefault();
        $(this).fadeOut('slow', function(){
            $(".check").show();
        });
    });
   $(".check").click(function(){
       if($(this).is(':checked')){
           $("#the_form").submit();
       }
   })
}) 
<input type="submit" value="Submit" class="submit_button">
<input type="checkbox" value="some value" style="display: none;" class="check">

Script example: 脚本示例:

$(document).ready(function(){
    $(".submit_button").click(function(){
        $(this).fadeOut('slow', function(){
            $(".check").show();
        });
    });

   $("check").click(function(){
       if($(this).is(':checked')){
           //Do action
       }
   })
}) 

Assuming you have HTML like this, wrapped in a form tag with id="form" 假设您有这样的HTML,包装在带有id="form"form标签中

<input id="chk" type="checkbox" /><label>Do you agree?</label> <br />
<input id="btn" type="submit" value="Submit" />

You can use some jQuery to detect the state on submit and modify the button text 您可以使用一些jQuery来检测提交状态并修改按钮文本

$("#form").submit(function () {
    var isChecked = $("#chk").prop("checked");
    var $btn = $("#btn");

    if (!isChecked) {
        $btn.attr("value", "I agree to the TOS");
        return false;
    }
});

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

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