简体   繁体   中英

parsing input field contents on blur using jquery and autocomplete

After a user has selected a value from an autocomplete list (Using jQuery autocomplete), I need to remove everything from the input box after the first space.

I got this far then got a bit lost. (currently just alerts on blur)

<script>
    $(function() {
        $( ".pn-autocomplete" ).autocomplete({
            source: "pn-json.php",
            minLength: 2,
        }).blur(function () {
            alert($(this).val());
        });
    });
</script>

Demo

Check this code snippet - It would skip all the text after the first space.

$(function () {
    $( ".pn-autocomplete" ).autocomplete({
        source: "pn-json.php",
        minLength: 2,
    }).blur(function () {
        $(this).val(function () {
            return this.value.split(' ')[0];
        });
    });
});

You can update the value to the first word like this,

<script>
    $(function() {
        $( ".pn-autocomplete" ).autocomplete({
            source: "pn-json.php",
            minLength: 2,
        }).blur(function () {
            var input_val = $(this).val(),
                first_word = input_val.replace(/(\w+).*/,"$1");
            $(this).val(first_word);
        });
    });
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM