图像为单选按钮

Image as a Radio button

本文关键字:单选按钮 图像      更新时间:2023-09-26

我想在表单中使用单选按钮。我正在使用AngularJS创建表单。但我想要图像而不是单选按钮。我可以通过添加css 来隐藏单选按钮

position: absolute;
left: -9999px;

但问题是它禁用了选中的事件。有什么方法可以让图片点击吗。

这是我的代码:

var app = angular.module("MyApp", []);
app.controller('MyCtrl', function($scope, $timeout) {         
  $scope.submitForm = function(isValid) {
    // check to make sure the form is completely valid
    if (isValid) {
      alert('our form is amazing');
      console.log(myform);
    }
  };
  $scope.sliderValue = null;
  $scope.name = '';    
  $scope.data = {
    singleSelect: null,
    multipleSelect: [],
    option1: 'option-1',
  };
  $scope.forceUnknownOption = function() {
    $scope.data.singleSelect = 'nonsense';
  };
});
<!DOCTYPE html>
<html ng-app="MyApp" lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
  </head>
  <body ng-controller="MyCtrl">
    <form name='myform' id="myform" ng-init="step = 1" ng-submit="submitForm(myform.$valid)">
      <div ng-show="step==1">
        <h3>Which step</h3>
        <div ng-form='step1form'>
          <input type="radio" name="step" ng-model="data.step" value="11" ng-disabled="!step1form.$valid" ng-click="step = 2">
          <img src="http://sstatic.net/stackoverflow/img/favicon.ico" style="width:50px" alt="Save icon"/>
          <p class="Text">
            Step 2
          </p>
        </div>
      </div>
      <div ng-show="step==2">
        <div ng-form='step2form'>
          <div ng-disabled="!step2form.$valid"><span>Finish</span></div>
        </div>
      </div>
    </form>
    <script>document.write("<base href='"" + document.location + "'" />");</script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.js"></script>
    <script src="script.js"></script>
  </body>
</html>

您需要将标签与您的无线电输入相关联,并将其与图像进行样式设置。您可以在这个演示中看到,当您设置标签样式时,它会代替输入

$(document).ready(function() {
  $('input').click(function() {
    alert($('input').val());
  });
});
label.radioLabel {
  background: pink;
  cursor: pointer;
  display: inline-block;
  height: 50px;
  width: 50px;
}
input[type=radio] {
  position: absolute;
  top: 0;
  left: -9999px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>No label</h1>
<input type="radio" name="step">
<h1>Label Wrapped Around</h1>
<label class="radioLabel">
  <input type="radio" name="step">
</label>
<h1>Label With "For"</h1>
<input type="radio" id="step" name="step">
<label class="radioLabel" for="step"></label>

显然,在标签上使用您自己的风格,但我建议保留cursor:pointer;,这样交互对您的用户来说是显而易见的。

尝试这个

    <label>
<input type="radio" name="step" ng-model="data.step" value="11" ng-disabled="!step1form.$valid" ng-click="step = 2">
<img src="http://sstatic.net/stackoverflow/img/favicon.ico" style="width:50px" alt="Save icon"/>
 </label>

css:

label > input{ /* HIDE RADIO */
  display:none;
}
label > input + img{ /* IMAGE STYLES */
  cursor:pointer;
  border:2px solid transparent;
}
label > input:checked + img{ /* (CHECKED) IMAGE STYLES */
  border:2px solid #f00;
}

https://jsbin.com/modotayufe/edit?html,css,js,输出

诀窍是用标签包装输入,这样当你点击它时,就像你点击了单选按钮。在标签中,放置一个span标签,这样您就可以将他的背景设置为您的图像。

在下面的片段中,您可以看到这一点。(我评论了ng-change属性,这样你就可以看到效果)

var app = angular.module("MyApp", []);
app.controller('MyCtrl', function($scope, $timeout) {	       	
  $scope.submitForm = function(isValid) {
    // check to make sure the form is completely valid
    if (isValid) {
      alert('our form is amazing');
      console.log(myform);
    }
  };
  $scope.sliderValue = null;
  $scope.name = '';    
  $scope.data = {
    singleSelect: null,
    multipleSelect: [],
    option1: 'option-1',
  };
  $scope.forceUnknownOption = function() {
    $scope.data.singleSelect = 'nonsense';
  };
});
input[type="radio"] {
  display:none;  
}
input[type="radio"] + span {
  content:"";
  background:url(https://i.stack.imgur.com/hlkG5.png);
  width:30px;
  height:30px;
  display:inline-block;
}
input[type="radio"]:checked + span {
  background-image:url(https://i.stack.imgur.com/TwN4q.png);  
}
<!DOCTYPE html>
<html ng-app="MyApp" lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
  </head>
  <body ng-controller="MyCtrl">
    <form name='myform' id="myform" ng-init="step = 1" ng-submit="submitForm(myform.$valid)">
      <div ng-show="step==1">
        <h3>Which step</h3>
        <div ng-form='step1form'>
          <label>
            <input type="radio" name="step" ng-model="data.step" value="11" ng-disabled="!step1form.$valid"><!--ng-click="step = 2"-->
            <span></span>
          </label>
          <img src="http://sstatic.net/stackoverflow/img/favicon.ico" style="width:50px" alt="Save icon"/>
          <p class="Text">
            Step 2
          </p>
        </div>
      </div>
      <div ng-show="step==2">
        <div ng-form='step2form'>
          <div ng-disabled="!step2form.$valid"><span>Finish</span></div>
        </div>
      </div>
    </form>
    <script>document.write("<base href='"" + document.location + "'" />");</script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.js"></script>
    <script src="script.js"></script>
  </body>
</html>

我使用了很棒的图标。你可以这样做:

<label class="hideRadio">
  <input class="" type="radio" value="" name="">
  <i class="fa fa-check-circle "></i>
</label>

并使用这个css:

.hideRadio input {
  visibility: hidden; /* Makes input not-clickable */
  position: absolute; /* Remove input from document flow */
}