Adding value to a input using function

痞子三分冷 提交于 2019-12-22 16:05:01

问题


I don't know what is getting wrong in my created function. I was trying to create a beautiful country selector. But it looks like this selector is gonna select nothing. Just joking, so my problem is according to my thinking my function should change the value of a input field classed as country_input on clicking multiple series of links. But it looks like it is not gonna work. But you guys can tell me how to achieve my goal.

-:::- HTML CODE -:::-

<input type="text" class="country_input" />
<br><br>
<a href="#country-select" id="Change-1" onclick="change_country();" >Country-1</a>
<a href="#country-select" id="Change-2" onclick="change_country();" >Country-2</a>
<a href="#country-select" id="Change-3" onclick="change_country();" >Country-3</a>
<a href="#country-select" id="Change-4" onclick="change_country();" >Country-4</a>
<a href="#country-select" id="Change-5" onclick="change_country();" >Country-5</a>
And so on....

-:::- jQuery CODE -:::-

jQuery.fn.change_country = function () {

                            var country_name = $(this).innerHTML;

                            $('input.country_input').val(country_name);

                          };

-:::- CSS CODE -:::-

body a { text-decoration: none; display: block; width: 50%; color: #555; background: rgb(240, 240, 240); border: 2px solid #000; border-style: none none solid none; font-family: calibri,segoe ui,arial, sans-serif; font-size: 12px; padding: 3px 5px; }

body a:hover { color: #000; background: #fff; }

body input { font-family: calibri,segoe ui,arial, sans-serif; font-size: 12px; padding: 3px 3px; width: 50%; outline: none; }

So can anyone help me out with this. As I'm a new comer in creating jQuery based functions so please help me out.

Am I doing wrong function defining? Am I missing something? Is my code is totally failure?

LIVE DEMO

THANKS IN ADVANCE!


回答1:


This works:

<script type="text/javascript">
$(document).ready(function(){
    $('.countryLink').click(function(){
        var innerText = $(this).text();
        $('.country_input').val(innerText);
    });
});
</script>

And HTML:

<input name="Text1" type="text" class="country_input"/>
<br/>
<a href="#country-select" id="Change-1" class="countryLink">Country-1</a>
<a href="#country-select" id="Change-2" class="countryLink">Country-2</a>
<a href="#country-select" id="Change-3" class="countryLink">Country-3</a>

See working example at jsfiddle.

Edit: I guess that you might want to use dropdown in scenario like this (too many options). Then you can do it like this:

$(document).ready(function(){
    $('.countryDropDown').change(function(){
        var innerText = $(this).children(':selected').text();
        $('.country_input').val(innerText);
    });
});

With HTML:

<input name="Text1" type="text" class="country_input"/>
<br/>
<select name="Select1" class="countryDropDown">
    <option value="c1">Country-1</option>
    <option value="c2">Country-2</option>
    <option value="c3">Country-3</option>
</select>



回答2:


Looks to me that the problem is you're adding the change_country function into jQuery, but when you're calling it, you're calling it like it was a normal global function.

It should work like you have it now if you change the first JS line to this:

var change_country = function() {

This would make it a global function, and then the call to it should work.

On a secondary note, all your a elements have the same ID - This is incorrect, as an element's ID should be unique.




回答3:


You could try this:

<input type="text" class="country_input" />
<br>
<div class="country-select">Country-1</div>
<div class="country-select">Country-2</div>
<div class="country-select">Country-3</div>
<div class="country-select">Country-4</div>

<script type="text/javascript">
$(function(){
    $(".country-select").click(function(){
        $('input.country_input').val($(this).innerHTML);
    });
});
</script>



回答4:


I think youre confused about how you want to implement this. Youve defined your function in the jQuery.fn namespace which is where you typcially would put a jQuery plugin that works on a set of DOM elements. But then you try and call that function directly with an onclick atribute on the element pointing to change_country which doesnt exist. If you were to call the function in this way it would actually be:

onclick="jQuery.fn.change_country()"

But i dont think thats what you really want to do. Heres how i would do it: http://jsfiddle.net/nWrAt/8/... more or less.



来源:https://stackoverflow.com/questions/5606443/adding-value-to-a-input-using-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!