Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.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
jquery函数检索所有元素,而不是特定元素_Jquery_Dom - Fatal编程技术网

jquery函数检索所有元素,而不是特定元素

jquery函数检索所有元素,而不是特定元素,jquery,dom,Jquery,Dom,在用户对订单排序并单击检查结果后,我使用以下函数从ul检索句子。我的问题是脚本会找到所有ul中包含的句子,而不仅仅是单击的特定句子 const answer = { Q1: "I can't play piano", Q2: "The moon is a green balloon" }; const QNo = 2; function checkO

在用户对订单排序并单击检查结果后,我使用以下函数从ul检索句子。我的问题是脚本会找到所有ul中包含的句子,而不仅仅是单击的特定句子

    const answer = {
                Q1: "I can't play piano",
                Q2: "The moon is a green balloon"                    
                };
const QNo = 2; 

function checkOrd() {
        // finds all instances of ul id: need to find one at a time and check only question clicked

        $('#quiz').find('ul').each(function() {
            let sentenceId = $(this).attr('id');
            //sentenceId is a str variable therefore must be applied as '#' + sentenceId to correctly function as id
            sentence = $.map($('#' + sentenceId + ' li'), function(element) {return $(element).text()}).join(' ');

            if (sentence == answer[sentenceId]) {
            alert('OK!') // message must be in quotes
                } else {
            alert('nope!')
            }

        });

    };
HTML:

    <div id = "quiz">
<ul class= "scramble" id = "Q1">
<li>I</li>
<li>piano</li>
<li>can't</li>
<li>play</li>
<button onclick ="checkOrd();">Check</button>
</ul>


<ul class="scramble" id = "Q2" >
<li>moon</li>
<li>green</li>
<li>is</li>
<li>balloon</li>
<li>a</li>
<li>The</li>
<button onclick ="checkOrd();">Check</button>
</ul>

</div>

  • 钢琴
  • 不能
  • 检查
  • 月亮
  • 绿色的
  • 气球
  • a
  • 检查
我尝试过循环,但所做的只是重复该函数。是否有方法将每个按钮唯一地链接到相邻的问题/答案对?在我看来,问题在于
语句=$.map($('#'+sentenceId+'li')、函数(元素){return$(元素).text()})
检索所有uls,因为它的编写方式。我不知道如何适当地改变它。我甚至不相信我已经温和地问了这个问题

试试这个

var字符串=“”;
$('#Q1>li,#Q2>li')。单击(函数(){
字符串+=$(this).text()+“”;
})
常数回答={
Q1:“我不会弹钢琴”,
问题2:“月亮是一个绿色的气球”
};
常数QNo=2;
函数checkOrd(){
if(string.split(“”).join(“”)=answer.Q1.split(“”)。join(“”)| | string.split(“”)。join(“”)=answer.Q2.split(“”)。join(“”))
{警报(“是”);字符串=''''}
其他的
{
警报(“否”);字符串=“”}
};

  • 钢琴
  • 不能
  • 检查
  • 月亮
  • 绿色的
  • 气球
  • a
  • 检查
试试这个

var字符串=“”;
$('#Q1>li,#Q2>li')。单击(函数(){
字符串+=$(this).text()+“”;
})
常数回答={
Q1:“我不会弹钢琴”,
问题2:“月亮是一个绿色的气球”
};
常数QNo=2;
函数checkOrd(){
if(string.split(“”).join(“”)=answer.Q1.split(“”)。join(“”)| | string.split(“”)。join(“”)=answer.Q2.split(“”)。join(“”))
{警报(“是”);字符串=''''}
其他的
{
警报(“否”);字符串=“”}
};

  • 钢琴
  • 不能
  • 检查
  • 月亮
  • 绿色的
  • 气球
  • a
  • 检查

我简化了您的代码并将其转换为jQuery,因为您已经在使用它了

const-answer={
Q1:“我不会弹钢琴”,
问题2:“月亮是一个绿色的气球”
};
常数QNo=2;
$(文档).ready(函数(){
$(文档).on(“单击“,”.btn单击”,函数(){
让sentenceId=$(this.nexist(“ul”).attr(“id”);
让句子=$.map($(this.closest(“ul”).find(“li”),函数(元素){return$(element.text()}).join(“”);
警报(句子+“\n”+答案[sentenceId]);
如果(句子==答案[句子ID]){
警报('OK!')//消息必须用引号括起来
}否则{
警报('nope!')
}
});
});

  • 钢琴
  • 不能
  • 检查
  • 月亮
  • 绿色的
  • 气球
  • a
  • 检查

我简化了您的代码并将其转换为jQuery,因为您已经在使用它了

const-answer={
Q1:“我不会弹钢琴”,
问题2:“月亮是一个绿色的气球”
};
常数QNo=2;
$(文档).ready(函数(){
$(文档).on(“单击“,”.btn单击”,函数(){
让sentenceId=$(this.nexist(“ul”).attr(“id”);
让句子=$.map($(this.closest(“ul”).find(“li”),函数(元素){return$(element.text()}).join(“”);
警报(句子+“\n”+答案[sentenceId]);
如果(句子==答案[句子ID]){
警报('OK!')//消息必须用引号括起来
}否则{
警报('nope!')
}
});
});

  • 钢琴
  • 不能
  • 检查
  • 月亮
  • 绿色的
  • 气球
  • a
  • 检查

首先要解决的问题是
不是
()的有效子级。其次,您需要将
onclick=“checkOrd();”
更改为
onclick=“checkOrd(this);”
,以便传入单击的元素,或者删除内联绑定并使用逻辑绑定,该绑定将传入可以使元素脱离的事件(event.target)。获取元素后,可以使用
最近('ul')
获取相关列表,然后从中获取嵌套的li元素。我不确定我是否理解如何执行您的建议。需要研究你的答案以及你提到的文件。感谢您,您应该解决的第一件事是
不是
()的有效子级。其次,您需要将
onclick=“checkOrd();”
更改为
onclick=“checkOrd(this);”
,以便传入单击的元素,或者删除内联绑定并使用逻辑绑定,该绑定将传入可以使元素脱离的事件(event.target)。获取元素后,可以使用
最近('ul')
获取相关列表,然后从中获取嵌套的li元素。我不确定我是否理解如何执行您的建议。需要研究你的答案以及你提到的文件。谢谢,麻烦的是我使用的“真正的”应用程序