[英]Unable to set the value of input field onclick
I have created this custom dropdown which has custom radio buttons. 我创建了具有自定义单选按钮的自定义下拉菜单。 I get the value of the selected radio button and set it to span tag successfully.
我获得了所选单选按钮的值,并将其设置为span标签成功。
What i am not getting is : i want to set the same value in the input field that is present in the dropdown, but the value is not getting set. 我没有得到的是:我想在下拉列表中出现的输入字段中设置相同的值,但是该值没有得到设置。
i use pure js as well as jquery to do so but the value is alerting and not setting into the input field. 我使用纯js以及jquery来执行此操作,但该值具有警报性,未设置到输入字段中。
code is live here: http://thekabir.in/onsitego-planlisting2017/index.html 代码位于此处: http : //thekabir.in/onsitego-planlisting2017/index.html
Steps: click on check brands dropdown and select any brand.. 步骤:单击检查品牌下拉菜单,然后选择任何品牌。
jquery and js used are 使用的jQuery和js是
//filters dropdown
function DropDown(el) {
this.dd = el;
this.placeholder = this.dd.children('span');
this.opts = this.dd.find('div.dropdown li');
this.val = '';
this.index = -1;
this.initEvents();
}
DropDown.prototype = {
initEvents : function() {
var obj = this;
obj.dd.on('click',function(){
$(this).toggleClass("active");
$(this).parent().toggleClass("border-active");
$('.filters').toggleClass('border-bottom');
$(this).children('.dropdown').css('width',$(window).width());
var deID = $(this);
if(deID[0].id == 'devicebrand')
{
$('#devicebrand i.icon-down-arrow-1').addClass('icon-up-arrow-1').removeClass('icon-down-arrow-1');
}
return false;
});
obj.opts.on('click',function(e){
// e.preventDefault();
$(this).parent().addClass('hidden');
$(this).addClass('active');
var opt = $(this);
obj.val = opt.text();
obj.index = opt.index();
obj.placeholder.text(obj.val);
var currentID = $(this).parents('div.wrapper-dropdown-3')[0].id;
if(currentID == 'devicebrand')
{
$('#devicebrand.wrapper-dropdown-3 .dropdown li').removeClass('active');
$('#devicebrand.wrapper-dropdown-3 .dropdown li span').removeClass('icon-selected-radio-yellow').addClass('icon-oval-3-copy-3');
$('#devicebrand i.icon-up-arrow-1').addClass('icon-tick-filled').removeClass('icon-up-arrow-1');
$('.more-brands').addClass('hidden');
$('.covered').removeClass('hidden');
$('#manual-brand-input').val(obj.val);
}
$(this).children('span').removeClass('icon-oval-3-copy-3').addClass('icon-selected-radio-yellow');
$(this).parent().toggleClass("border-active");
$('.dropdown input').val('');
e.stopPropagation();
});
},
getValue : function() {
return this.val;
},
getIndex : function() {
return this.index;
}
};
$(function() {
var dd = new DropDown( $('#devicebrand') );
$(document).click(function() {
// all dropdowns
$('.wrapper-dropdown-3').removeClass('active');
});
});
Html : HTML:
<div class="filter check-brand">
<div id="devicebrand" class="wrapper-dropdown-3" tabindex="1">
<i class="icon icon-brand"></i>
<span data-val="-1">check brand </span>
<div class="dropdown brand">
<span class="icon-cross-it"></span>
<div class="side-info">
<p class="SELECT-YOUR-MOBILE-P">SELECT YOUR MOBILE BRAND </p>
<p class="One-line-to-explain">One line to explain why he needs to select his city. Along with more information.</p>
</div>
<div class="city-selection">
<ul>
<li><span class="icon-oval-3-copy-3"></span><a href="#" data-val="htc">HTC</a></li>
<li><span class="icon-oval-3-copy-3"></span><a href="#" data-val="motorola">motorola</a></li>
<li><span class="icon-oval-3-copy-3"></span><a href="#" data-val="xiaomi">xiaomi</a></li>
<li><span class="icon-oval-3-copy-3"></span><a href="#" data-val="lg">LG</a></li>
<li><span class="icon-oval-3-copy-3"></span><a href="#" data-val="samgsung">samgsung</a></li>
<li><span class="icon-oval-3-copy-3"></span><a href="#" data-val="sony">sony</a></li>
<li><span class="icon-oval-3-copy-3"></span><a href="#" data-val="huawei">huawei</a></li>
<li><span class="icon-oval-3-copy-3"></span><a href="#" data-val="google pixel">google pixel</a></li>
<li><span class="icon-oval-3-copy-3"></span><a href="#" data-val="nokia">nokia</a></li>
<li><span class="icon-oval-3-copy-3"></span><a href="#" data-val="le-eco">le-eco</a></li>
</ul>
<div class="more-brands">
<a class="more-brands-btn" href="javascript:void(0);">+ 254 Brands</a>
</div>
<div class="manual-brand">
<input placeholder="Enter your brand if not found above " id="manual-brand-input" class="manual-brand-input ui-autocomplete-input" value="" autocomplete="off" type="text"><span class="icon-shape_4"></span>
<ul id="ui-id-1" tabindex="0" class="ui-menu ui-widget ui-widget-content ui-autocomplete ui-front" style="display: none;"></ul></div>
<div class="covered hidden">
<p><span class="icon-tick"></span>Congratulations! Free Pick & Drop service available in your city</p>
<p><span class="icon-tick"></span>400 Service centers near you</p>
<p><span class="icon-tick"></span>20% of people bought this near your locality</p>
<p><span class="icon-tick"></span>3days is average repair time in your area.</p>
</div>
<div class="not-covered hidden">
<p><span class="icon-not-covered-tick"></span>Sorry, we are currently present in India. We don’t cover your City.</p>
<p>We can briefly state here why we dont cover particular city. Or if at all we are in process of including it.</p>
</div>
</div>
</div>
<i class="icon-down-arrow-1"></i>
<i class="icon-orange-cross hidden"></i>
</div>
</div>
Here is the fiddle for the same. 这是相同的小提琴。 https://jsfiddle.net/kvab7wyd/1/
https://jsfiddle.net/kvab7wyd/1/
UPDATE UPDATE
Applied my answer to OP's Fiddle 将我的答案应用于OP的小提琴
Store your value in a variable, then set the input's value equal to the variable. 将您的值存储在变量中,然后将输入的值设置为等于该变量。 The way you had it was not working because
document.getElementById('input').value
is plain JavaScript expression and $(this).attr(data-)
is jQuery expression, if you mix them, you must take dereference the jQuery by using $(obj).get()
or dot notation $(obj)[0]
, although I'm not entirely sure it would be worth the trouble in doing so. 您无法使用它的方式是因为
document.getElementById('input').value
是普通的JavaScript表达式,而$(this).attr(data-)
是jQuery表达式,如果将它们混合使用,则必须通过以下方式取消对jQuery的引用:使用$(obj).get()
或点表示法$(obj)[0]
,尽管我不确定这样做是否值得。 BTW use e.preventDefault()
when using links that don't go anywhere, that'll stop that irritating jumping when clicking them. 顺便说一句,当使用不存在任何链接的链接时,请使用
e.preventDefault()
,这将阻止单击它们时e.preventDefault()
跳跃。 I also used .data()
instead of .attr()
it's the same result either way but .data()
looks cleaner. 我还使用了
.data()
而不是.attr()
尽管结果相同,但是.data()
看起来更干净。
SNIPPET SNIPPET
$('.dropdown li a').on('click', function(e){ var data = $(this).data('val'); e.preventDefault(); document.getElementById('manual-brand-input').value = data; });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="filter check-brand"> <div id="devicebrand" class="wrapper-dropdown-3" tabindex="1"> <i class="icon icon-brand"></i> <span data-val="-1">check brand </span> <div class="dropdown brand"> <span class="icon-cross-it"></span> <div class="side-info"> <p class="SELECT-YOUR-MOBILE-P">SELECT YOUR MOBILE BRAND </p> <p class="One-line-to-explain">One line to explain why he needs to select his city. Along with more information.</p> </div> <div class="city-selection"> <ul> <li><span class="icon-oval-3-copy-3"></span><a href="#" data-val="htc">HTC</a></li> <li><span class="icon-oval-3-copy-3"></span><a href="#" data-val="motorola">motorola</a></li> <li><span class="icon-oval-3-copy-3"></span><a href="#" data-val="xiaomi">xiaomi</a></li> <li><span class="icon-oval-3-copy-3"></span><a href="#" data-val="lg">LG</a></li> <li><span class="icon-oval-3-copy-3"></span><a href="#" data-val="samgsung">samgsung</a></li> <li><span class="icon-oval-3-copy-3"></span><a href="#" data-val="sony">sony</a></li> <li><span class="icon-oval-3-copy-3"></span><a href="#" data-val="huawei">huawei</a></li> <li><span class="icon-oval-3-copy-3"></span><a href="#" data-val="google pixel">google pixel</a></li> <li><span class="icon-oval-3-copy-3"></span><a href="#" data-val="nokia">nokia</a></li> <li><span class="icon-oval-3-copy-3"></span><a href="#" data-val="le-eco">le-eco</a></li> </ul> <div class="more-brands"> <a class="more-brands-btn" href="javascript:void(0);">+ 254 Brands</a> </div> <div class="manual-brand"> <input placeholder="Enter your brand if not found above " id="manual-brand-input" class="manual-brand-input ui-autocomplete-input" value="" autocomplete="off" type="text"><span class="icon-shape_4"></span> <ul id="ui-id-1" tabindex="0" class="ui-menu ui-widget ui-widget-content ui-autocomplete ui-front" style="display: none;"></ul></div> <div class="covered hidden"> <p><span class="icon-tick"></span>Congratulations! Free Pick & Drop service available in your city</p> <p><span class="icon-tick"></span>400 Service centers near you</p> <p><span class="icon-tick"></span>20% of people bought this near your locality</p> <p><span class="icon-tick"></span>3days is average repair time in your area.</p> </div> <div class="not-covered hidden"> <p><span class="icon-not-covered-tick"></span>Sorry, we are currently present in India. We don't cover your City.</p> <p>We can briefly state here why we dont cover particular city. Or if at all we are in process of including it.</p> </div> </div> </div> <i class="icon-down-arrow-1"></i> <i class="icon-orange-cross hidden"></i> </div> </div>
Main change is here: $('.dropdown input').val($(this).find('a').data('val')); 主要变化在这里:$('。dropdown input')。val($(this).find('a')。data('val'));
//filters dropdown
function DropDown(el) {
this.dd = el;
this.placeholder = this.dd.children('span');
this.opts = this.dd.find('div.dropdown li');
this.val = '';
this.index = -1;
this.initEvents();
}
DropDown.prototype = {
initEvents : function() {
var obj = this;
obj.dd.on('click',function(){
$(this).toggleClass("active");
$(this).parent().toggleClass("border-active");
$('.filters').toggleClass('border-bottom');
$(this).children('.dropdown').css('width',$(window).width());
var deID = $(this);
if(deID[0].id == 'devicebrand')
{
$('#devicebrand i.icon-down-arrow-1').addClass('icon-up-arrow-1').removeClass('icon-down-arrow-1');
}
return false;
});
obj.opts.on('click',function(e){
// e.preventDefault();
$(this).parent().addClass('hidden');
$(this).addClass('active');
var opt = $(this);
obj.val = opt.text();
obj.index = opt.index();
obj.placeholder.text(obj.val);
var currentID = $(this).parents('div.wrapper-dropdown-3')[0].id;
if(currentID == 'devicebrand')
{
$('#devicebrand.wrapper-dropdown-3 .dropdown li').removeClass('active');
$('#devicebrand.wrapper-dropdown-3 .dropdown li span').removeClass('icon-selected-radio-yellow').addClass('icon-oval-3-copy-3');
$('#devicebrand i.icon-up-arrow-1').addClass('icon-tick-filled').removeClass('icon-up-arrow-1');
$('.more-brands').addClass('hidden');
$('.covered').removeClass('hidden');
}
$(this).children('span').removeClass('icon-oval-3-copy-3').addClass('icon-selected-radio-yellow');
$(this).parent().toggleClass("border-active");
console.log($(this).data('val'));
$('.dropdown input').val($(this).find('a').data('val'));
e.stopPropagation();
});
},
getValue : function() {
return this.val;
},
getIndex : function() {
return this.index;
}
};
$(function() {
var dd = new DropDown( $('#devicebrand') );
$(document).click(function() {
// all dropdowns
$('.wrapper-dropdown-3').removeClass('active');
});
});
You can try this: 您可以尝试以下方法:
$('.dropdown li a').click(function(e){
$("#manual-brand-input").val($(this).attr('data-val'));
});
There was a small error in my code. 我的代码中有一个小错误。
$('.dropdown input').val('');
this was causing the value to be empty. 这导致该值为空。
Sorry guys. 对不起大家。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.