Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 获取URL';从jQuery中的字符串中删除域_Javascript_Jquery - Fatal编程技术网

Javascript 获取URL';从jQuery中的字符串中删除域

Javascript 获取URL';从jQuery中的字符串中删除域,javascript,jquery,Javascript,Jquery,所以我有一个文本框,用户可以写一个URL 我有一个下拉菜单,上面有国家的国旗和代码 我想实现的是,当用户键入URL时,标志将更改为URL的域。示例:如果用户键入google.com,下拉列表应选择点后的值,选择美国国旗 我正在使用这个库: 以下是我的HTML代码: <form method="post"> <input type="hidden" name="addsite" value="true" /> <p> <la

所以我有一个文本框,用户可以写一个URL

我有一个下拉菜单,上面有国家的国旗和代码

我想实现的是,当用户键入URL时,标志将更改为URL的域。示例:如果用户键入google.com,下拉列表应选择点后的值,选择美国国旗

我正在使用这个库:

以下是我的HTML代码:

<form method="post">
    <input type="hidden" name="addsite" value="true" />
    <p>
        <label for="site_url">Site url:</label>
        <input type="text" name="site_url" id="urlText" placeholder="domain.xxx" value=""  />
    </p>
    <label for="site_url">Search locale:</label>
    <input id="country_selector" type="text">
    <label for="site_url"></label>
    <br>
    <br>
    <input type="submit" name="submit" class="btn" value="Add">
</form>


网站网址:

搜索区域设置:

以下是脚本:

//the script to initalize the dropdown
$("#country_selector").countrySelect({
    defaultCountry: "dk",
    //onlyCountries: ['us', 'gb', 'ch', 'ca', 'do'],
    preferredCountries: ['dk', 'gb', 'us', 'ca']
});

//script to change the dropdown value
$('#urlText').change(function changeCountry(string selectedCountryValue) {
    $("#country_selector").countrySelect("selectCountry", $selectedCountryValue);
});

//script I've attempted to write
(function($) {
    $('#urlText').on('change', function() {
        var value = this.value,
            parts = this.value.split('.'),
            str, $opt;
        for (var i = 0; i < parts.length; i++) {
            str = '.' + parts.slice(i).join('.');
            $opt = $('#country_selector selectCountry[value="' + str + '"]');
            if ($opt.length) {
                $opt.prop('selected', true);
                break;
            }
        }
    })
})(jQuery);
//初始化下拉列表的脚本
$(“#国家/地区选择器”)。国家/地区选择({
默认国家:“dk”,
//仅限国家:['us'、'gb'、'ch'、'ca'、'do'],
首选国家:['dk','gb','us','ca']
});
//用于更改下拉列表值的脚本
$('#urlText').change(函数changeCountry(字符串selectedCountryValue){
$(“#国家/地区选择器”).countrySelect(“selectCountry”,$selectedCountryValue);
});
//我试图写的剧本
(函数($){
$('#urlText')。在('change',function()上{
var值=此.value,
parts=此.value.split('.'),
str$opt;
对于(变量i=0;i
所以最后两个脚本是错误的。我如何编写它们,使changeCountry函数接受一个字符串selectedValue,然后最后一个函数应该调用changeCountry(str)


编辑:我想我可能犯了一些基本的jQuery错误,甚至…

下面是一个如何获取url的域后缀部分的示例

$('btnGetUrlDomain')。在('click',function()上{
var parts=$('#txtrl').val().split('.'))
变量域=零件[parts.length-1]
console.log(域)
})

获取URL

我需要的是:

$('#urlText').on('change', function() {
  var parts = $('#urlText').val().split('.')
  var domain = parts[parts.length - 1]
    $("#txtUrls").val(domain);
    $("#country_selector").countrySelect("selectCountry", domain);
})

我运行了你的代码,但我只收到了.com部分,并且在url中添加了一个文件扩展名,仍然会把它搞砸(就像他们经常做的那样)。域是domain.xxx,而不仅仅是“.xxx”部分。域的“www”部分是子域,但www是一种引用。这实际上没有影响任何事情。我不确定我是否完全理解你想做什么。你的例子说“如果用户输入google.com,下拉列表应该选择点后的值,选择美国国旗。”-点后的值应该是“com”?可能想在评论部分发表你对他实际问题的评论,而不是你的答案。我认为他不会像对这个问题那样收到对答案发表评论的通知。@TyQ。从你编辑添加后缀我明白你现在说的。我认为OP可能和我犯了同样的语义错误,我仍然认为我的输出是OP所要求的。如果不是,我肯定他/她会评论。我正在阅读评论,别担心,试着让它像我想的那样工作:)