Semantic UI - How to get data of active Input Element?

蓝咒 提交于 2019-12-13 08:47:35

问题


how can I get the value of the input field to the urlData Object?

<input id="username">

And the JS is:

$('#username').api({
   action: 'check username',
    urlData: {username: $(this).val()},
    onSuccess: function(response){
        console.log(response)
    },
    onError: function(error){
        console.log(error)
    }
});`

With normal JQuery i can get it with an $(this).val();. But in this case it´s empty. How does it work with Semantic UI ?


回答1:


You can try like this

 $( "#username" ).keyup(function() {
    var value=$(this).val();
    $('#username').api({
       action: 'check username',
        urlData: {username: value},
        onSuccess: function(response){
            console.log(response)
        },
        onError: function(error){
            console.log(error)
        }
    });
 });



回答2:


for anyone who can't figure out why the request does not run immediately (typed 'abc' then the request just 'ab'), here's the solution!


(I'm going to using example above) You can just add

on:'now'

explicitly make semanticjs api to do request 'now', as the keyup(or any events) handle the rest.

$( "#username" ).keyup(function() {
var value=$(this).val();
$('#username').api({
   action: 'check username',
   on:'now',
    urlData: {username: value},
    onSuccess: function(response){
        console.log(response)
    },
    onError: function(error){
        console.log(error)
    }
});
});

I decide to answer this pretty old question, might be someone needs it!



来源:https://stackoverflow.com/questions/32456380/semantic-ui-how-to-get-data-of-active-input-element

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