英文:
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 -->
$("#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>
<!-- 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 = $('#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>
<!-- 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 = $('#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);
<!-- 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>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论