如何在用户填写表单时显示数据?

huangapple go评论54阅读模式
英文:

How to display data as the user fills the form?

问题

我试图做什么

我正在寻找一种方法,以在用户填写字段时显示其输入内容。

到目前为止我尝试过的

我可以在我的第一个示例中轻松做到这一点,即如果用户填写第一个字段,它将被显示,如果填写第二个下拉菜单,所选值将显示在旁边。

<!-- language: lang-js -->
$("#mytext").on('input', function () {
   var mytext =  $(this).val();
   $("#result").find('.a').html(mytext+"?");
 });

 $("#myselect").on('change', function () {
    myselect  = $(this).find('option:selected').val();  
     $("#result").find('.b').html("utm_source="+myselect);
 });

<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js" integrity="sha512-STof4xm1wgkfm7heWqFJVn58Hm3EtS31XFaagaa8VMReCXAkQnJZ+jEy8PCC/iT18dFy95WcExNHFTqLyp72eQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

<form id="fistform">
  <input id="mytext" type="text">

  <select id="myselect" style="" name="select[]" class="select">
     <option selected disabled="disabled">select your product</option>
     <option value="https://google.com/">My product</option>
     <option value="https://google.fr/">My second product</option>
  </select>
</form>

<div id="result">
 <span class="a"></span><span class="b"></span>
</div>

我遇到的问题

问题是我有很多字段和下拉菜单。因此,我找到了一种将代码连接起来的方法,这是我的第二个示例,但问题在于一切都一次性显示出来。

<!-- language: lang-js -->
 function onChange(){
      var mysecondtext = $('#mysecondtext').val();
      var mysecondselect = $('#mysecondselect option:selected').val();
    
    const value = mysecondtext + '?' + 'utm_source=' + mysecondselect
  
  $('#secondresult').html(value);
}

$('#secondform select').on('change',onChange);
$('#secondform input').on('input', onChange);

<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js" integrity="sha512-STof4xm1wgkfm7heWqFJVn58Hm3EtS31XFaagaa8VMReCXAkQnJZ+jEy8PCC/iT18dFy95WcExNHFTqLyp72eQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

<form id="secondform">
<input id="mysecondtext" type="text">

<select id="mysecondselect" style="" name="select[]" class="select">
	<option selected disabled="disabled">select your product</option>
     <option value="https://google.com/">My product</option>
     <option value="https://google.fr/">My second product</option>
</select>
</form>

<div id="secondresult">

</div>

所以我正在尝试使用第二个选项,一种只在用户填写字段或从下拉菜单中选择选项时显示用户输入的方法。这是一个语法问题,我不知道在哪里以及如何放置条件:“如果变量包含数据,显示它”。

英文:

What I'm trying to do

I'm looking for a way to show user input as they fill in the fields

What I've tried so far

I can easily do this in my first example, i.e. if the user fills in the first field, it is displayed, and if he fills in the second drop-down menu, the selected value is displayed next

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&quot;#mytext&quot;).on(&#39;input&#39;, function () {
   var mytext =  $(this).val();
   $(&quot;#result&quot;).find(&#39;.a&#39;).html(mytext+&quot;?&quot;);
 });

 $(&quot;#myselect&quot;).on(&#39;change&#39;, function () {
    myselect  = $(this).find(&#39;option:selected&#39;).val();  
     $(&quot;#result&quot;).find(&#39;.b&#39;).html(&quot;utm_source=&quot;+myselect);
 });

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js&quot; integrity=&quot;sha512-STof4xm1wgkfm7heWqFJVn58Hm3EtS31XFaagaa8VMReCXAkQnJZ+jEy8PCC/iT18dFy95WcExNHFTqLyp72eQ==&quot; crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot;&gt;&lt;/script&gt;





&lt;form id=&quot;fistform&quot;&gt;
  &lt;input id=&quot;mytext&quot; type=&quot;text&quot;&gt;

  &lt;select id=&quot;myselect&quot; style=&quot;&quot; name=&quot;select[]&quot; class=&quot;select&quot;&gt;
	 &lt;option selected disabled=&quot;disabled&quot;&gt;select your product&lt;/option&gt;
   &lt;option value=&quot;https://google.com/&quot;&gt;My product&lt;/option&gt;
   &lt;option value=&quot;https://google.fr/&quot;&gt;My second product&lt;/option&gt;
  &lt;/select&gt;
&lt;/form&gt;

&lt;div id=&quot;result&quot;&gt;
 &lt;span class=&quot;a&quot;&gt;&lt;/span&gt;&lt;span class=&quot;b&quot;&gt;&lt;/span&gt;
&lt;/div&gt;

<!-- end snippet -->

The problem I'm having

The problem is that I have a lot of field and drop-down menu. So I found a way to concatenate the code, this is my second example, but the problem here is that everything is showing at once.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

 function onChange(){
      var mysecondtext = $(&#39;#mysecondtext&#39;).val();
      var mysecondselect = $(&#39;#mysecondselect option:selected&#39;).val();
    
    const value = mysecondtext + &#39;?&#39; + &#39;utm_source=&#39; + mysecondselect
  
  $(&#39;#secondresult&#39;).html(value);
}

$(&#39;#secondform select&#39;).on(&#39;change&#39;,onChange);
$(&#39;#secondform input&#39;).on(&#39;input&#39;, onChange);

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js&quot; integrity=&quot;sha512-STof4xm1wgkfm7heWqFJVn58Hm3EtS31XFaagaa8VMReCXAkQnJZ+jEy8PCC/iT18dFy95WcExNHFTqLyp72eQ==&quot; crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot;&gt;&lt;/script&gt;


&lt;form id=&quot;secondform&quot;&gt;
&lt;input id=&quot;mysecondtext&quot; type=&quot;text&quot;&gt;

&lt;select id=&quot;mysecondselect&quot; style=&quot;&quot; name=&quot;select[]&quot; class=&quot;select&quot;&gt;
	&lt;option selected disabled=&quot;disabled&quot;&gt;select your product&lt;/option&gt;
     &lt;option value=&quot;https://google.com/&quot;&gt;My product&lt;/option&gt;
     &lt;option value=&quot;https://google.fr/&quot;&gt;My second product&lt;/option&gt;
&lt;/select&gt;
&lt;/form&gt;

&lt;div id=&quot;secondresult&quot;&gt;

&lt;/div&gt;

<!-- end snippet -->

So I'm looking using the second option, a way to display user input only if he fills in the field or selects an option from the drop down menu(s).
It's a syntax problem, I don't know where and how to place the condition : "if the variable contains data, display it"

答案1

得分: 0

尝试这个解决方案,我希望它能解决你的问题:

function onChange(){
  var mysecondtext = $('#mysecondtext').val();
  var mysecondselect = $('#mysecondselect option:selected').val();

  var value = '';

  if(mysecondtext != ''){
    value += mysecondtext + '?';
  }

  if(mysecondselect != 'select your product'){
    value += 'utm_source=' + mysecondselect;
  }

  $('#secondresult').html(value);
}

$('#secondform select').on('change', onChange);
$('#secondform input').on('input', onChange);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js" integrity="sha512-STof4xm1wgkfm7heWqFJVn58Hm3EtS31XFaagaa8VMReCXAkQnJZ+jEy8PCC/iT18dFy95WcExNHFTqLyp72eQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

<form id="secondform">
  <input id="mysecondtext" type="text">

  <select id="mysecondselect" style="" name="select[]" class="select">
    <option selected disabled="disabled">select your product</option>
    <option value="https://google.com/">My product</option>
    <option value="https://google.fr/">My second product</option>
  </select>
</form>

<div id="secondresult">

</div>
英文:

try this solution i hope it will solve your problem

<!-- begin snippet: js hide: false console: true babel: null -->

try this update may it help

<!-- language: lang-js -->

 function onChange(){
      var mysecondtext = $(&#39;#mysecondtext&#39;).val();
      var mysecondselect = $(&#39;#mysecondselect option:selected&#39;).val();
    
    var value = &#39;&#39;;

    if(mysecondtext != &#39;&#39;){
      value += mysecondtext + &#39;?&#39;;
    }

    if(mysecondselect != &#39;select your product&#39;){
      value += &#39;utm_source=&#39; + mysecondselect;
    }
  
  $(&#39;#secondresult&#39;).html(value);
}

$(&#39;#secondform select&#39;).on(&#39;change&#39;,onChange);
$(&#39;#secondform input&#39;).on(&#39;input&#39;, onChange);

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js&quot; integrity=&quot;sha512-STof4xm1wgkfm7heWqFJVn58Hm3EtS31XFaagaa8VMReCXAkQnJZ+jEy8PCC/iT18dFy95WcExNHFTqLyp72eQ==&quot; crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot;&gt;&lt;/script&gt;


&lt;form id=&quot;secondform&quot;&gt;
&lt;input id=&quot;mysecondtext&quot; type=&quot;text&quot;&gt;

&lt;select id=&quot;mysecondselect&quot; style=&quot;&quot; name=&quot;select[]&quot; class=&quot;select&quot;&gt;
	&lt;option selected disabled=&quot;disabled&quot;&gt;select your product&lt;/option&gt;
     &lt;option value=&quot;https://google.com/&quot;&gt;My product&lt;/option&gt;
     &lt;option value=&quot;https://google.fr/&quot;&gt;My second product&lt;/option&gt;
&lt;/select&gt;
&lt;/form&gt;

&lt;div id=&quot;secondresult&quot;&gt;

&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月10日 07:05:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75405370.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定