问题描述
我有一个功能,如果用户点击一个按钮,它会在文本框中显示一个值,并在点击'#btn'按钮后执行另一个功能的触发:
I have a function where if the user clicks on a button, it will display a value in the textbox and it will perform a trigger to another function after a '#btn' button is clicked:
function addwindow(numberAnswer, gridValues, btn) {
$('#mainNumberAnswerTxt').val(numberAnswer).data('data-ignore',true);
$('#btn'+gridValues).trigger('click');
}
现在我想做的是:
-
如果用户点击添加按钮,则将答案数列中的数字显示到文本框中,换句话说,执行此操作来自
addwindow()
函数:
$('#mainNumberAnswerTxt')。val (numberAnswer).data('data-ignore',true);
如果用户点击了 #btn + gridValues
按钮,然后在文本框中显示当前打开的按钮数量的数字,或者换句话说执行此代码:
If the user has clicked on a #btn+gridValues
button, then display the number in the textbox of the number of buttons which are currently turned on or in other words perform this code:
if($('#mainNumberAnswerTxt')。data('data-ignore')!= true){
$('。answertxt',context).val (context.find('。answerBtnsOn')。length> 0?context.find('。answerBtnsOn')。length:0);
}
问题是第1步工作正常,它确实在用户点击添加按钮后,显示文本框中答案数列中的数字。
The problem is that step 1 works fine, it does display the number from the "Number of Answers" column in the textbox after the user has clicked on the "Add" button.
问题是第2步,它不是在用户点击#btn + gridValues
按钮后,显示当前打开的按钮数量的正确数字。它不会更改文本框中的数字。
The problem is step 2, it is not displaying the correct number on how many buttons are currently turned on after the user has clicked on the #btn+gridValues
button.It just doesn't change the number in the textbox.
有谁知道为什么会这样,以及如何修复?
Does anyone know why this is happening and how thi can be fixed?
DEMO:
这是申请。请按照以下步骤操作:
Here is a demo of the application. Please follow the steps below:
步骤1:在左侧,您将看到一个绿色的加号按钮,点击它,它会打开一个模态窗口。
步骤2:在模态窗口中有一个搜索栏,输入AAA并提交搜索,你会看到一堆行出现。
步骤3:在最后一行中,您会在Number of Answer列中看到它包含数字4,单击此行中的添加按钮,模态窗口将关闭。
Step 1: On left hand side you will see a green plus button, click on it and it opens up a modal window.Step 2: In modal window there is a search bar, type in "AAA" and submit search, you will see a bunch of rows appear.Step 3: In the last row, you see under "Number of Answer" colum that it contains the number 4, click on the "Add" button within this row, the modal window will close.
您将看到文本框中的文本框显示数字4,这是正确的,因为这是您添加行时答案数列下行中的数字。
You will see that the textbox displays the number 4 in the textbox which is fine as that was the number within the row under the "Number of Answers" column when you added the row.
但问题如下:
步骤4:如果单击打开网格链接并选择按钮3,您将看到下面的字母按钮更改为AC,只打开字母B。
Step 4: If you click on the "Open Grid" link and select button 3, you will see the letter buttons below change to A-C with only letter "B" turned on.
在文本框中,它应显示数字1,因为只有1个按钮被打开打开,但它没有显示这个数字,这就是我遇到的问题。
In the textbox it should display number 1 as only 1 button is turned on, but it doesn't display this number and that is the problem I am having.
如何解决这个问题?
推荐答案
问题来自于你在 $('#mainNumberAnswerTxt')上将data-attribute设置为false的事实
但是你正在办理登机手续g对输入(点击中此
)。
The problem comes from the fact taht you're setting the data-attribute to false on the $('#mainNumberAnswerTxt')
but you're checking against the inputs (this
in the click).
一种解决方案是进行以下测试 if($('#mainNumberAnswerTxt')。attr('data-ignore')!= true)
One solution would be to do the following test if ($('#mainNumberAnswerTxt').attr('data-ignore') != true)
编辑
为了确保忽略过程只执行一次(当模态后重新加载网格时),你必须改变里面的内容。测试后阻止:
To ensure that the ignore process execute only once (when the grid is reloaded after the modal), you'll have to change what's inside the block after the test:
if ($('#mainNumberAnswerTxt').data('ignore') != true) {
$('.answertxt', context).val(context.find('.answerBtnsOn').length > 0 ? context.find('.answerBtnsOn').length : 0);
} else {
/*reset the ignore flag*/
$('#mainNumberAnswerTxt').data('ignore',false);
}
EDIT2
主要问题是您在设置后重新影响 $('#mainNumberAnswerTxt')。val(numberAnswer)
。你试图通过忽略
属性忽略它。
The main problem is that your reaffecting the $('#mainNumberAnswerTxt').val(numberAnswer)
after setting it. Your trying to ignore it through the ignore
attribute.
我想要的是删除每一次出现该属性并更改以下函数
What i would adivse, would be to remove every occurence to that attribute and to change the following function
function addwindow(questionText,textWeight,numberAnswer,gridValues,reply,btn) {
var answers = $.map(btn.split(''),function(chr){ return "#answer"+chr; }).join(', ');
if($(plusbutton_clicked).attr('id')=='mainPlusbutton') {
var myNumbers = {};
myNumbers["A-C"] = "3";
myNumbers["A-D"] = "4";
myNumbers["A-E"] = "5";
myNumbers["A-F"] = "6";
myNumbers["A-G"] = "7";
gridValues = myNumbers[gridValues];
$('#mainNumberAnswerTxt').show();
$('#mainNumberAnswerTxt').val(numberAnswer).data('ignore',true);
$('#mainGridTxt').val(gridValues).parent().append($('.optionTypeTbl'));
$('#btn'+gridValues).trigger('click');
$('.answers.answerBtnsOn').removeClass('answerBtnsOn').addClass('answerBtnsOff');
$(answers).addClass("answerBtnsOn").siblings().addClass('answerBtnsOff');
}
$.modal.close();
return false;
}
to:
function addwindow(questionText,textWeight,numberAnswer,gridValues,reply,btn) {
var answers = $.map(btn.split(''),function(chr){ return "#answer"+chr; }).join(', ');
if($(plusbutton_clicked).attr('id')=='mainPlusbutton') {
var myNumbers = {};
myNumbers["A-C"] = "3";
myNumbers["A-D"] = "4";
myNumbers["A-E"] = "5";
myNumbers["A-F"] = "6";
myNumbers["A-G"] = "7";
gridValues = myNumbers[gridValues];
$('#mainNumberAnswerTxt').show();
$('#mainGridTxt').val(gridValues).parent().append($('.optionTypeTbl'));
$('#btn'+gridValues).trigger('click');
/* moved the following line below the click simulation,
hence its value will be changed regardless of what happened during the click simulation*/
$('#mainNumberAnswerTxt').val(numberAnswer);
$('.answers.answerBtnsOn').removeClass('answerBtnsOn').addClass('answerBtnsOff');
$(answers).addClass("answerBtnsOn").siblings().addClass('answerBtnsOff');
}
$.modal.close();
return false;
}
为了在视觉变化发生后修改价值。
In order to modify the value after the visual changes took place.
这篇关于代码在不应该生效时生效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!