好的,我知道我的标题不是很丰富。让我解释。从另一个选择框中选择带有Li元素的框
有一个页面,我想用jQuery来完成。在他们的页面(旧页面)上,有一个选择的下拉菜单,我将它带到我的(新)页面。他们的选择下拉列表使用ul元素和li元素,而我的select使用选项元素。
以下是我有:
var totalQuestions = jQuery('.rcbItem').length; //get the total elements in theirs
for (x = 0; x < totalQuestions; x++) //for loop to create the dropdown on mine
{
var questionText = jQuery('.rcbItem:eq(' + x + ')').html();
jQuery('#selectQuestion').append(jQuery(document.createElement('option')).attr(
{
'value' : "Question" + x
}).html(questionText));
}
var strQuestion = jQuery('#ctl00_ContentPlaceHolder1_ddlQuestion_Input').val(); //gets the value currently in THEIR select field
var objQuestionNext = jQuery("#selectQuestion").find("option:contains('" + strQuestion + "')");
objQuestionNext.attr(
{
selected : 'true'
}); //finds the question in MY list that matches THEIR select field, and then selects that one
这是一个运行每次我从我的下拉列表中选择一个问题时的代码:
jQuery('#selectQuestion').change(function()
{
strQuestion = jQuery('#selectQuestion :selected').val(); //get the value of "option", which is Question + x
strAnswer = jQuery('#txtQuestionAnswer').val();
intQuestionNumber = strQuestion.substr(8, 2).trim(); //gets the number (so Question + x becomes x)
jQuery('#ctl00_ContentPlaceHolder1_ddlQuestion_Arrow')[0].click(); //I click THEIR dropdown menu
jQuery('.rcbItem:eq(' + (intQuestionNumber-1) + ')').click(); //Select THEIR li element that matches MINE)
jQuery('#ctl00_ContentPlaceHolder1_ctbAnswer').val(strAnswer);
});
这工作90%的时间。
但是,如果我选择列表中最后一个选项,则会发生这种情况。它选择它,这很好,但是如果我然后从列表中选择另一个问题,它会开始选择一个我想要的,然后继续这样做,直到我完全重新加载页面。这只有在我选择我的下拉列表中最后一个元素时才会发生。
我一直在关注这件事,并没有找到答案。谁能提供一些见解?谢谢。
编辑:我终于找到了问题。在他们的代码中,当点击发生时,每个元素所拥有的.rcbItem类都被.rcbHover类覆盖。所以要解决它,我基本上改变.rcbItem为jQuery('.rcbList li')....
,它现在完美。
谢谢你的帮助球员。
你尝试过调试它,看看它是怎么回事了?你能在http://www.jsfiddle.net上创建一个演示吗? – 2013-02-12 15:31:03
至少向我们展示一些HTML标记会有所帮助。 – kidwon 2013-02-12 15:32:24
不幸的是,我没有任何HTML给你。我在页面上唯一触及的是Javascript。他们的东西在ASP中被严格编码,而不是HTML,我不能碰它们,因为它是他们的代码。 – snowfi6916 2013-02-12 15:36:33