Can you upload a random file every time you click the same button using jQuery?

I have 4 contact forms that we would like to turn for testing. I don’t want to run them in separate campaigns, and for such a simple thing this is overkill. Is it possible to have a group of 4 forms and load them randomly (another form) every time you press a button?

$(function() {
    $("a.random")click.(function){
    //?
    });
});
+3
source share
5 answers

Yes.

<form id="form0" class="test_form">
<form id="form1" class="test_form" style="display: none;">
<form id="form2" class="test_form" style="display: none;">
<form id="form3" class="test_form" style="display: none;">

$(function() {
    $("a.random")click.(function){
       $(".test_form").hide();
       var formNumber = Math.floor(Math.random() * 4);//will equal a number between 0 and 3
       $("#form" + formNumber).show();
    });
});
+3
source
var n = Math.floor( Math.random() * 4 ) + 1;

This will give you a random integer from 1 to 4. Based on this, you run different commands.

+3
source

:

<div id="forms">
  <form style="display:none;" ...>
    ...
  </form>
  <form style="display:none;" ...><!-- the second form -->
    ...
  </form>
  ...
</div>
<a href="#" id="randomForm">Show random form</a>

jQuery:

$('#randomForm').click(function() {
    var forms = $('#forms > form');
    forms.hide();
    forms.eq(Math.floor(Math.random() * forms.length)).show();
});

, .

+1

, - :

$(function() {
    $("a.random")click.(function){
        var randomnumber=Math.floor(Math.random()*5)
        switch (randomnumber) {
          case 1:
           $(this).attr("href",[url 1]);
           break;
          case 2:
           $(this).attr("href",[url 2]);
           break;
          case 3:
           $(this).attr("href",[url 3]);
           break;
          default:
           $(this).attr("href",[url 4]);
           break;
        }
    });
});
0
source

Can we use pure javascript and dom here, see if it works for you.

var arr = [form1, form2, form3, form4];

var fn = function(arr){
  var cur_frm = arr[Math.floor(Math.random() * arr.length)];
  cur_frm.style.display = 'block';

  for(i=0; i<arr.length;i++){
    if(arr[i] != cur_frm) arr[i].style.display = 'none';
  }

}
0
source

Source: https://habr.com/ru/post/1778141/


All Articles