Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.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需要If语句帮助_Javascript_Jquery_Html_If Statement_Autocomplete - Fatal编程技术网

javascript需要If语句帮助

javascript需要If语句帮助,javascript,jquery,html,if-statement,autocomplete,Javascript,Jquery,Html,If Statement,Autocomplete,我使用的是autocomplete,其中搜索的每个变量都有3个属性标签、id和值。我希望用户选择的值根据该选择的id传递到四个隐藏字段之一 js文件 var destinations= [ {value: "25",label: "USA",id: "1"}, {value: "26",label: "Midwest",id: "2"}, {value: "27",label: "Colorado",id: "3"}, {value

我使用的是autocomplete,其中搜索的每个变量都有3个属性标签、id和值。我希望用户选择的值根据该选择的id传递到四个隐藏字段之一

js文件

var destinations= [
        {value: "25",label: "USA",id: "1"},
        {value: "26",label: "Midwest",id: "2"},
        {value: "27",label: "Colorado",id: "3"},
        {value: "28",label: "Denver",id: "4"}
        ];

$(document).ready(function() {
    $( "#destinations" ).autocomplete({
        minLength: 1,
        source: destinations,

        select: function( event, ui ) {
            $('#destinations').val( ui.item.label );
            $('#destinationid').val(ui.item.id);
            $('#destinationtype').val(ui.item.value);
            if ($("#destinationid").val() == 1){
            $("#results1").text($('#type1').val());
        }
        if ($("#destinationid").val() == 2){
            $("#results2").text($('#type2').val());
        }
        if ($("#destinationid").val() == 3){
            $("#results3").text($('#type3').val());
        }
        if ($("#destinationid").val() == 4){
            $("#results4").text($('#type4').val());
        }
    },

        focus: function( event, ui ) {
            $("#destinations").val( ui.item.label );
            return false;
        }
    });

});
在我的html中,我有以下内容:我没有为了测试目的而隐藏它们

<input id="destinations" /> 
<input type="hidden" input id="type1" /> 
<input type="hidden" input id="type2" />
<input type="hidden" input id="type3" />
<input type="hidden" input id="type4" /> 
“自动选择”会很好地拖动标签,但当我选择一个标签时,它会将选择更改为其值,而不是保留标签。它也不是将值放在相应的文本框中,而是将值保留在第一个文本框中

如有任何建议,将不胜感激

var destinations= [
        {value: "25",label: "USA",id: "1"},
        {value: "26",label: "Midwest",id: "2"},
        {value: "27",label: "Colorado",id: "3"},
        {value: "28",label: "Denver",id: "4"}
        ];



$(document).ready(function() {
    $( "#destinations" ).autocomplete({
        minLength: 1,
        source: destinations,

        select: function( event, ui ) {
                $('#destinations').val( ui.item.label );
                var destinationId = u.item.id;
                $('#destinationvalue').val(ui.item.value);
                $('#type' + destinationId).val(ui.item.value);
        },

        focus: function( event, ui ) {
            $("#destinations").val( ui.item.label );
            return false;
        }
    });

});

您的输入标签中有一个额外的输入。。。我不确定您的意思是它是为了测试还是什么,但这肯定会导致一些不必要的行为。

在select语句中返回false,否则jquery UI的默认select将在您的语句之后运行


工作代码的演示:

多亏Jason给我指出了正确的方向,我才能够理解它。它还不完美,但它做了我想做的

我觉得我的js文件的逻辑有问题。好吧,我是不是把一些东西标记为java而不是javascript?是的。您可以查看修订历史记录。我将把它标记为javascript;在if语句中,不应使用=;您需要使用==进行比较。为什么要将输入作为每个元素的属性?变量destinationid是什么?你没有在任何地方声明。我使用你的第一个建议进行了更改,但仍然收到相同的结果。它将第一个框更改为值,并将其他4个文本框留空。如果要将文本框的值更新为所选内容或标签的id,是否应使用$'type1'.val$destinationid.val?查看我的编辑。我已编辑,但现在我无法从自动选择提示中实际选择项目,以查看其他字段是否已填充。提示会出现,但无法选择。嘿,伙计,谢谢你的帮助。我对你的代码做了一些调整就可以让它工作了。回答如下。嘿,谢谢你把它放在一起,但是如果我从类型1-4输入中去掉隐藏语句,我看不到任何值填充。有什么想法吗?
<input type="hidden" input id="type1" />