jQuery:如何将变量分配回文档中的隐藏元素?

发布于 2024-11-01 05:32:41 字数 1376 浏览 0 评论 0原文

如何将变量(单击的按钮 id)分配给 jQuery Diaglog,然后返回到文档中的隐藏元素(myhiddenid)以供以后使用?

<table border="0" class="time_data">
    <td><button type="button" id='001' class="del-fruit" > Apple </td>
    <td><button type="button" id='002' class="del-fruit" > Banana </td>
    <td><button type="button" id='003' class="del-fruit" > Cantalope </td>
</table>

<div id='myhiddenid' style="display:none;"></div>

<script type="text/javascript">
$("#dialog-form").dialog({
    autoOpen: false,
    height: 150,
    width: 350,
    modal: true,
    resizable: false,
    buttons: {
        'Yes': function() {
            var bValid = true;
            allFields.removeClass('ui-state-error');

            bValid = bValid 

            if (bValid) {
                //the assining of myhiddenid should be here!!
                //and should contain the ID of the button that i clicked.
                $(this).dialog('close');
            }
        },
        No: function() {
            $(this).dialog('close');
        }
    },
    close: function() {
        allFields.val('').removeClass('ui-state-error');
    }
});


$('.del-fruit')
    .button()
    .click(function() {
    $('#dialog-form')
        .dialog( "option", "title", this.id )
        .dialog('open');
});
</script>

How can I assign a variable (button id that was clicked), to the jQuery Diaglog, then back to the hidden element (myhiddenid) in the document for later use?

<table border="0" class="time_data">
    <td><button type="button" id='001' class="del-fruit" > Apple </td>
    <td><button type="button" id='002' class="del-fruit" > Banana </td>
    <td><button type="button" id='003' class="del-fruit" > Cantalope </td>
</table>

<div id='myhiddenid' style="display:none;"></div>

<script type="text/javascript">
$("#dialog-form").dialog({
    autoOpen: false,
    height: 150,
    width: 350,
    modal: true,
    resizable: false,
    buttons: {
        'Yes': function() {
            var bValid = true;
            allFields.removeClass('ui-state-error');

            bValid = bValid 

            if (bValid) {
                //the assining of myhiddenid should be here!!
                //and should contain the ID of the button that i clicked.
                $(this).dialog('close');
            }
        },
        No: function() {
            $(this).dialog('close');
        }
    },
    close: function() {
        allFields.val('').removeClass('ui-state-error');
    }
});


$('.del-fruit')
    .button()
    .click(function() {
    $('#dialog-form')
        .dialog( "option", "title", this.id )
        .dialog('open');
});
</script>

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

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

发布评论

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

评论(3

榕城若虚 2024-11-08 05:32:41

您可以使用隐藏字段并为其分配值,而不是 div:例如

HTML

<input type="hidden" id="hdnId" />

jQuery

$("#hdnId").val(id);

Instead of a div, you can use hidden field and assign it's value: For e.g.

HTML

<input type="hidden" id="hdnId" />

jQuery

$("#hdnId").val(id);
小女人ら 2024-11-08 05:32:41

同意第一个答案,但如果你想使用隐藏的 div:

set:

$("div#myhiddenid").html("whateverId");

get:

var foo = $("div#myhiddenid").html();

Agreed with first answer, but if you want to use a hidden div:

set:

$("div#myhiddenid").html("whateverId");

get:

var foo = $("div#myhiddenid").html();
腹黑女流氓 2024-11-08 05:32:41

另一种方法是使用 jQuery .data() 函数来存储针对元素的信息,例如针对 #dialog-form 本身,并使用它来表示从对话框返回的信息。如果您发现自己需要返回的不仅仅是一个简单的 ID,这会很方便,因为您可以直接存储对象,而不必从隐藏字段来回序列化信息。

以下是基于 HTML 的示例: http://jsbin.com/omexi5

当文档加载时,每个按钮表中的 被分配了一个单击处理程序,该处理程序首先将其 ID 记录在对话框的 .data() 中,然后打开对话框。对话框的“打开”事件处理程序使用 .data() 中的 ID 在对话框内设置消息,然后“是”处理程序使用它来查找和更新表按钮的文本。

希望有帮助。

An alternative approach would be to use the jQuery .data() function to store information against an element, for instance against the #dialog-form itself, and use it to represent the information coming back from the dialog. This would be handy if you found yourself needing to return more than just a simple ID, because you could store an object directly rather than having to serialise the information back and forth from hidden fields.

Here is an example based on your HTML: http://jsbin.com/omexi5

When the document loads, each button in the table is assigned a click handler that first records it's ID in the dialog's .data() and then it opens the dialog. The dialog's 'open' event handler uses the ID from .data() to set the message inside the dialog and then the Yes handler uses it to find and update the table button's text.

Hope it helps.

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