简体   繁体   English

单击添加按钮后如何显示应答按钮?

[英]How to show answer buttons after add button has been clicked on?

I have an application here where an user is able to select their options and answers. 我在这里有一个应用程序,用户可以选择他们的选项和答案。 The best way to explain you the question is if you use the application itself so you can see what is happening. 向您解释这个问题的最佳方法是您是否使用应用程序本身,以便了解正在发生的事情。 Please follow steps below in application. 请按照下面的步骤申请。

  • Step 1: When you open application, you will see the "Open Grid" link, click on it and select an option type, after you select an option it will display the answer buttons at the bottom. 第1步:打开应用程序时,将看到“打开网格”链接,单击它并选择一个选项类型,选择一个选项后,它将在底部显示答案按钮。 For example if the option you choose is "5", it will display 5 answer buttons "A - E", if option chose is 8, it will display 8 answer buttons "AH". 例如,如果您选择的选项是“ 5”,它将显示5个应答按钮“ A-E”,如果所选择的选项是8,则将显示8个应答按钮“ AH”。

Now this is fine. 现在很好。 But the problem I have is if the user wants to add a previous option. 但是我的问题是用户是否要添加上一个选项。 please look at steps below: 请查看以下步骤:

  • Step 2: Refresh the page 步骤2:刷新页面
  • Step 3: You will see a green plus button on left hand side, click on it, this will open up a modal window. 步骤3:您会在左侧看到一个绿色的加号按钮,单击它,将打开一个模态窗口。
  • Step 4: In the search box type in "AAA" and then click on "Submit" button, it will display rows from the database. 步骤4:在搜索框中键入“ AAA”,然后单击“提交”按钮,它将显示数据库中的行。
  • Step 5: Please select a row by clicking on the "Add" button, the modal window will close and if you look at the answer and option control on the right hand side, you can see that the Option Type textbox and Number of Answers textbox appears, but the "Answer" buttons do not appear. 步骤5:请点击“添加”按钮选择一行,模态窗口将关闭,如果您查看右侧的答案和选项控件,您会看到“选项类型”文本框和“答案数量”文本框出现,但不显示“答案”按钮。

So my question is how can I get the "Answer" buttons to appear after the user has clicked on the "Add" button? 所以我的问题是,用户单击“添加”按钮后如何显示“答案”按钮?

Below is function where it controls what happens after the "Add" button has been clicked on: 下面是控制单击“添加”按钮后会发生什么的功能:

function addwindow(numberAnswer,gridValues) { 

    if(window.console) console.log();

    if($(plusbutton_clicked).attr('id')=='mainPlusbutton') { 


        $('#mainNumberAnswerTxt').val(numberAnswer);
        $('#mainGridTxt').val(gridValues);
        } else { 
            $(plusbutton_clicked).closest('tr').find('input.numberAnswerTxtRow').val(numberAnswer);
            $(plusbutton_clicked).closest('tr').find('input.gridTxtRow').val(gridValues);
            }

    $.modal.close(); 
    return false;
} 

Below is the code where the answer buttons are stored (What do I need to call on to be able to show the answer buttons): 下面是存储答案按钮的代码(我需要调用什么才能显示答案按钮):

<table id="optionAndAnswer" class="optionAndAnswer">

...

<?php
    $a = range("A","Z");
?>

<table id="answerSection">
    <tr>

<?php
    $i = 1;
    foreach($a as $key => $val){
        if($i%7 == 1) echo"<tr><td>";
        echo"<input type=\"button\" onclick=\"btnclick(this);\" value=\"$val\" id=\"answer".$val."\" name=\"answer".$val."Name\" class=\"answerBtns answers answerBtnsOff\">";      
        if($i%7 == 0) echo"</td></tr>";
        $i++;
    }
?>
    </tr>
    <tr>
<td>
<input class="answerBtns answers" name="answerTrueName"  id="answerTrue"    type="button"   value="True"    onclick="btnclick(this);"/>
<input class="answerBtns answers" name="answerFalseName" id="answerFalse"   type="button"   value="False"   onclick="btnclick(this);"/>
<input class="answerBtns answers" name="answerYesName"   id="answerYes"     type="button"   value="Yes"     onclick="btnclick(this);"/>
<input class="answerBtns answers" name="answerNoName"    id="answerNo"      type="button"   value="No"      onclick="btnclick(this);"/>
</td>
</tr>
</table>
...
</table>

UPDATE: 更新:

Below is the code where it imports the answer buttons after an option is selected from the grid: 下面是从网格中选择一个选项后将其导入答案按钮的代码:

       $('.gridBtns').on('click', function()

    {

    appendAnswers(this);

});

function appendAnswers(t){
var clickedNumber = t.value;

        $(t).closest('.option').siblings('.numberAnswer').find('.answertxt').show();
        $(t).closest('.option').siblings('.numberAnswer').find('.string').hide();



         $(t).closest('.option').siblings('.answer').find('.answers').each(function(index) {
            if (!isNaN(clickedNumber) && index < clickedNumber) {
                $(t).show();
                $(t).closest('.option').siblings('.numberAnswer').find('.string').hide();


         // show but don't change the value
         $(t).closest('.option').siblings('.numberAnswer').find('.answertxt').show();


            } else {

                $(t).hide();
                $(t).removeClass('answerBtnsOn');
                $(t).addClass('answerBtnsOff');

            }

            var $this = $(t);
             var context = $this.parents('.optionAndAnswer');
             console.log($this);


        });

        prevButton = clickedNumber;


            $(t).closest('.option').siblings('.noofanswers').find('.string').hide();     
            $(t).closest('.option').siblings('.noofanswers').find('.answertxt').show();


        getButtons();

    };
});



    function getButtons()
    {
        var i;
        if (initClick == 0) {
            for (i = 65; i <= 90; i++) { // iterate over character codes for A to Z
                $("#answer" + String.fromCharCode(i)).removeClass("answerBtnsOn").addClass("answerBtnsOff");

            }

            initClick = 1;
        }
        // code above makes sure all buttons start off with class answerBtnsOff, (so all button are white).
    }

I'm not really too sure if I understand your question completely, but I tried out your application a few times and I think I understand: Load a result from the database -> populate fields with the result ? 我不太确定我是否能完全理解您的问题,但是我尝试了几次您的应用程序,我想我知道:从数据库中加载结果->用结果填充字段?

Anyway, I see you're using Jquery, so I think you should be able to use .html() or .append() to add each button in a for loop: (I assume you've stored your answer buttons in an array). 无论如何,我看到您正在使用Jquery,所以我认为您应该能够使用.html()或.append()在for循环中添加每个按钮:(我假设您已将答案按钮存储在数组中)。 I'm lazy so I usually name targets with an ID #ButtonHolder is your for the buttons. 我很懒,所以我通常使用ID为目标命名#ButtonHolder是您用于按钮的名称。

var buttonhtml = ""; //I personally like to build the html then insert it. 
var lastchar = gridValues.charCodeAt(gridvalues.length-1);
for (i=65;i<=lastchar;i++) // 65 is the code for "A"
buttonhtml += "<input type=\"button\" onclick=\"btnclick(this);\" value=\""+String.fromCharCode(i)+"\" id=\"answer"+String.fromCharCode(i)+"\" name=\"answer"+String.fromCharCode(i)+"Name\" class=\"answerBtns answers answerBtnsOn\">";
$("#ButtonHolder").html(buttonhtml);

Have you thought about instead of using onclick, using JQuery to handle the clicks with a class? 您是否考虑过使用JQuery通过类处理单击而不是使用onclick?

Edit: 编辑:

function addwindow(questionText,textWeight,numberAnswer,gridValues) { 



    if(window.console) console.log();

    if($(plusbutton_clicked).attr('id')=='mainPlusbutton') { 

        if( gridValues != "True or False" && gridValues != "Yes or No" )
{
   var myNumbers = {};
   myNumbers["A-C"] = "3";
   myNumbers["A-D"] = "4";
   myNumbers["A-E"] = "5";
//...
   myNumbers["A-Z"] = "26";
   gridValues = myNumbers[gridValues];
   $('#mainNumberAnswerTxt').show();

}
else{
   $('#mainNumberAnswerTxt').hide();
}

        $('#mainTextarea').val(questionText); 
        $('#mainTxtWeight').val(textWeight);
        $('#mainNumberAnswerTxt').val(numberAnswer);
        $('#mainGridTxt').val(gridValues);
        } else { 
            $(plusbutton_clicked).closest('tr').find('input.numberAnswerTxtRow').val(numberAnswer);


$(plusbutton_clicked).closest('tr').find('input.gridTxtRow').val(gridValues);

    var lastchar = gridValues.charCodeAt(gridValues.length-1); // getting the last charcode i.e. "A-D" -> "D" -> 68
   $(".ansBtns").removeClass("answerBtnsOn").addClass("answerBtnsOff"); // reset all the buttons to off *I think this works* 
    // 65 is the code for "A" Iterating each choice and activating it:
    for (i=65;i<=lastchar;i++)
    $("#answer"+String.charCodeAt(i)).addClass("answerBtnsOn");    
        $.modal.close(); 
        return false;
    } 

Well, whats firing when you click on an option in the grid? 好吧,当您单击网格中的一个选项时,会触发什么? if the name of the object that has the properties of what your looking for is named based off of the option type: ie "option type 4" is "op4" then, instead of having the "Previous Questions" query populate the field, have it store the values it returns as vars, then just call the same function that populates the answers when you click the option in the popup: 如果根据选项类型来命名具有您要查找的属性的对象的名称:即“选项类型4”为“ op4”,则不必在字段中填充“先前的问题”查询,而应使用它将返回的值存储为var,然后单击弹出菜单中的选项时,只需调用填充答案的函数即可:

optionPicked("op" + opType);

or however you called it. 还是不管你怎么称呼它。

EDIT: "this" needed to be passes as an object, try edited code 编辑:“ this”需要作为对象传递,请尝试编辑代码

Pull out the function from the onClick like this and then call it like "getChoice(4);" 像这样从onClick中提取函数,然后像“ getChoice(4);”那样调用它 when the history page fires the add button. 当历史记录页面触发添加按钮时。

Rewrite like this: 像这样重写:

function getChoice(obj){

    var clickedNumber = this.value;

    $(obj).closest('.option').siblings('.numberAnswer').find('.answertxt').show();
    $(obj).closest('.option').siblings('.numberAnswer').find('.string').hide();



     $(obj).closest('.option').siblings('.answer').find('.answers').each(function(index) {
        if (!isNaN(clickedNumber) && index < clickedNumber) {
            $(obj).show();
            $(obj).closest('.option').siblings('.numberAnswer').find('.string').hide();


     // show but don't change the value
     $(obj).closest('.option').siblings('.numberAnswer').find('.answertxt').show();


        } else {

            $(obj).hide();
            $(obj).removeClass('answerBtnsOn');
            $(obj).addClass('answerBtnsOff');

        }

        var $this = $(obj);
         var context = $this.parents('.optionAndAnswer');
         console.log($this);


    });


    getButtons();

}



$('.gridBtns').on('click', function(){

getChoice(this);
});

Just call the same function as you use for the click on a .gridBtns. 只需调用与您单击.gridBtns相同的函数即可。 Make the function as his own function, and call it with the property (this) so it knows what data to use. 将函数设为自己的函数,并使用属性(this)对其进行调用,以使其知道要使用的数据。 I guess that's the solver. 我想这就是解决方案。 Just run that function again (maybe you have to modify it a bit in order to work for both buttons) 只需再次运行该功能(也许您需要对其进行一些修改才能同时使用两个按钮)

In order to do this. 为此。 Make to eventlisteners, one for .gridBtns and one for the .addBtn. 制作事件监听器,一个用于.gridBtns,一个用于.addBtn。

When .gridBtns or .addBtns is called, just run the function in the eventlistener as: 调用.gridBtns或.addBtns时,只需在eventlistener中运行以下函数:

$('.gridBtns').on('click', function(){
  appendAnswers(this);
});

and modify your function you used before to something like: 并将您以前使用的功能修改为:

function appendAnswers(t){
  //t = this you defined in the onclick event
  //rest of your code here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM