Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.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/69.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使用函数事件之外的变量_Javascript_Jquery - Fatal编程技术网

JavaScript使用函数事件之外的变量

JavaScript使用函数事件之外的变量,javascript,jquery,Javascript,Jquery,我对java脚本相当陌生。如果这是个愚蠢的问题,我很抱歉 我有一些accordion元素的JavaScript函数,该函数可以正常工作。但是我想当我点击任何accordion元素时,获得该accordion的id属性,如您在click函数事件中所看到的,然后将该id放在第二个函数的contentTypeValue选择器之前 showHideUniMul函数不起作用,我认为这是由于itemId变量造成的。在使用showHideUniMul函数之前,如何在click事件函数之外使用itemId 信息

我对java脚本相当陌生。如果这是个愚蠢的问题,我很抱歉

我有一些accordion元素的JavaScript函数,该函数可以正常工作。但是我想当我点击任何accordion元素时,获得该accordion的id属性,如您在click函数事件中所看到的,然后将该id放在第二个函数的contentTypeValue选择器之前

showHideUniMul函数不起作用,我认为这是由于itemId变量造成的。在使用showHideUniMul函数之前,如何在click事件函数之外使用itemId

信息当我不使用itemId变量并且itemId成功返回到id属性时,showHideUniMul函数可以正常工作

$('ul#menu-to-edit li a.item-edit').click(function() {
    var itemId = $(this).parent().parent().parent().parent().attr('id');
});

var showHideUniMul = function() {
    var contentTypeValue = $(itemId + ' .field-content-type input:checked').val();
    if (contentTypeValue === 'uniform') {
       //some codes here
    } else if (contentTypeValue === 'multiple') {
       //some codes here
    } else {
       //some codes here
    }
};

showHideUniMul();
contentType.on('change', showHideUniMul);
编辑

这是我的Html结构

    <ul id="menu-to-edit">
    <li id="1">
        <div>
            <div>
                <span>
                    <a class="item-edit">Click Me and then check radio boxes</a>
                </span>
            </div>
        </div>
        <div>
            <p class="field-content-type">
                <input type="radio" name="uniform[1]" value="uniform" />
                <input type="radio" name="multiple[1]" value="multiple" />
            </p>
        </div>
    </li>
    <li id="2">
        <div>
            <div>
                <span>
                    <a class="item-edit">Click Me and then check radio boxes</a>
                </span>
            </div>
        </div>
        <div>
            <p class="field-content-type">
                <input type="radio" name="uniform[2]" value="uniform" />
                <input type="radio" name="multiple[2]" value="multiple" />
            </p>
        </div>
    </li>
</ul>

您的问题是因为您的作用域未在showHideUniMul函数中定义itemId。将您的代码更改为以下内容-

var itemId;

$('ul#menu-to-edit li a.item-edit').click(function() {
    itemId = $(this).parent().parent().parent().parent().attr('id');
});

var showHideUniMul = function() {
    var contentTypeValue = $('#' + itemId + ' .field-content-type input:checked').val();
    if (contentTypeValue === 'uniform') {
       //some codes here
    } else if (contentTypeValue === 'multiple') {
       //some codes here
    } else {
       //some codes here
    }
};

showHideUniMul();
contentType.on('change', showHideUniMul);
这使得itemId是全局的,并且可以从任何函数进行访问。您还需要将+itemId添加到contentTypeValue的值中。注意:-函数执行完成后,由于局部作用域,在函数内创建的变量将消失,因此您不能直接将它们用于其他函数

您需要在此处使用全局变量范围/概念:-

var itemId; // define global

$('ul#menu-to-edit li a.item-edit').click(function() {
    itemId = $(this).parent().parent().parent().parent().attr('id');
});

var showHideUniMul = function() {
    var contentTypeValue = $(itemId + ' .field-content-type input:checked').val();
    if (contentTypeValue === 'uniform') {
       //some codes here
    } else if (contentTypeValue === 'multiple') {
       //some codes here
    } else {
       //some codes here
    }
};

showHideUniMul();
contentType.on('change', showHideUniMul);
一个示例:-

var itemId;//定义全局 $'ulmenu-to-edit li a.item-edit'。单击功能{ itemId=$this.parent.parent.parent.parent.attr'id'; }; var showHideUniMul=函数{ var contentTypeValue=$”。字段内容类型输入:选中“.val; console.logitemId; }; $'input[type=radio]'。在'change'上,函数{ 炫耀; }; 单击我,然后选中收音机框

单击我,然后选中收音机框


请同时添加相关的HTML。如果变量定义在不同的范围内,则不能使用该变量。因此,为了做到这一点,您需要在一个双方都能看到的范围内定义变量。单击事件与该函数有何关系?你的意思是在点击事件中调用该函数吗?@David我想根据id属性调用showHideUniMul为什么这会被否决?这是正确的答案,假设你在点击时正确地获得了itemId。除了范围问题之外,我正在考虑这个问题。存储元素而不是id是有意义的,这样就不需要额外的查找了。你可以直接找到它。@Taplar是的,这将是一个更好的选择,但它肯定取决于那里使用的代码的结构和深度级别。@AlivetoDie谢谢兄弟,现在它可以工作了perfectly@SAFEEN1990很高兴帮助您::