如何在这种情况下使用 .on() 函数

发布于 2024-12-22 09:08:52 字数 560 浏览 0 评论 0原文

在我的应用程序的顶部,用户可以选择他们的选项和答案,然后他们可以将他们的选项和答案存储在新的表行中。

我的问题是如何让每一行执行与上面的选项和答案功能相同的功能,以便如果用户改变主意更改特定行中的选项或答案,他们可以在该行中更改它。

我的代码位于 JSFiddle 上。如果您希望能够使用小提琴中的选项和答案功能,请按照以下步骤操作。

  1. 打开网格并选择选项“3”。 “3”显示在文本框中,按钮 A 到 C 显示在下面。

  2. 在答案数文本框中输入数字 2。这意味着按钮 A 到 C 之间只能选择 2 个按钮。

  3. 选择按钮 A 和 B,如果您尝试选择另一个按钮,则会出现警报,说明您超出限制。

  4. 单击“添加问题”按钮,将创建一行显示您的详细信息。

我希望每列中的所有功能都与顶部选项和答案功能的功能相匹配。我知道我需要使用 .on() 函数,但如何为每一行包含此函数?

At the top of my application the user is able to choose their options and answers and then they can store their options and answers in a new table row.

What my question is how can I have each and every row to perform the same functionality as the options and answers features above so that if the user changes their mind on changing an option or answer in a particular row, they can change it within the row.

My code is on JSFiddle. Please follow the steps below if you want to be able to use the options and answers feature in the fiddle.

  1. Open grid and select option "3". "3" is displayed in textbox and buttons A to C are displayed below.

  2. Type in number 2 in the Number of Answers textbox. This means only 2 buttons can be selected between buttons A to C.

  3. Select buttons A and B, if you try to select another button an alert would appear stating you are beyond limit.

  4. Click on "Add Question" button and a row would be created showing your details.

I want all of the functionality in each column to match the functionality of the options and answers feature on top. I know I need to use the .on() function but how can I include this for each row?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看透却不说透 2024-12-29 09:08:52

如果我理解正确,您想知道如何在步骤“3”上重用关于表行的“警报”。

那么您是对的 - “on”是您想要的功能。 (此函数弃用了 live 函数
“on”所做的是重新评估每个事件的选择器。

而不是在中分配“onclick”事件

<input class="answerBtns" name="answerAName" id="answerA" type="button" value="A" onclick="btnclick(this);"/>

您应该编写以下内容,

$(".answerBtns").on("click",btnclick); 

这将有效地将“btnclick”方法绑定到此页面具有或将具有(现在和将来)的每个“.answerBtns”元素上 - 因为选择器 $(".answerBtns ") 将在每次点击时重新评估。

现在剩下的就是概括“btnclick”函数以支持所有场景。这意味着它必须识别它是在行中还是在表格中,并表现得适当。

例如(未选中,但不是伪代码):

<script>
  function btnclick(){
      if ( $(this).closest("#qandatbl").length > 0 ) {  // we are in the table because we are wrapped in a table
           var myRow = $(this).closest("tr");
           var ourLimit = myRow.find(".numberAnswerTxtRow").val();
           if ( myRow.find(".answerBtnsOn").length > ourLimit ){
                 alert("...");... 
           }
      }else{...} // we are not in a table.. lets do what we always did. 
  }
</script>

如您所见,我使用了 closestfind 因为它们为我提供了相同的功能,但仅相对于点击原点。

如果我遗漏了什么,请告诉我,我会添加它。

If I understand correctly, you want to know how to reuse the "alert" on step "3" in regard to the table rows..

Then you are correct - "on" is the function you want. ( this function deprecate the live function )
What "on" does is re-evaluate the selector on each event.

Instead of assigning the "onclick" event in

<input class="answerBtns" name="answerAName" id="answerA" type="button" value="A" onclick="btnclick(this);"/>

You should write the following

$(".answerBtns").on("click",btnclick); 

This will effectively bind the "btnclick" method on every ".answerBtns" element this page has or will have (present and future) - since the selector $(".answerBtns") will be re-evaluated on each click.

Now all that is left is to generalize the "btnclick" function to support the all scenarios. Which means it has to identify if it is in a row or in the form and behave appropriately.

For example (unchecked, but not pseudo code):

<script>
  function btnclick(){
      if ( $(this).closest("#qandatbl").length > 0 ) {  // we are in the table because we are wrapped in a table
           var myRow = $(this).closest("tr");
           var ourLimit = myRow.find(".numberAnswerTxtRow").val();
           if ( myRow.find(".answerBtnsOn").length > ourLimit ){
                 alert("...");... 
           }
      }else{...} // we are not in a table.. lets do what we always did. 
  }
</script>

As you can see, I used closest and find because they give me same functionality but only relative to the click origin.

Let me know if I missed something and I will add it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文