我们可以从变量设置控件的值

时间:2012-06-06 06:53:11

标签: jquery jquery-selectors controls

是否可以设置控件ID保存在变量中的控件的值。代码看起来像

var textmonth = $(this).parent().find('input[id$=txtmonths]').attr("id");

我正在尝试做这样的事情

textmonth.val("Only numbers");

真的有可能吗?

演示代码如下:

html代码是:

<div id="pagecontent">
<h1>
        Collection Master</h1>

    <table cellpadding="1" cellspacing="2" style="padding-left:40px; font-size:medium; padding-right:20px"
        width="100%" border="0">
        <tr>
                <td colspan="2" style="width: 100%">
                    <asp:GridView ID="grdFees" runat="server" AllowPaging="false" CssClass="Grid" AutoGenerateColumns="false"><Columns>

                            <asp:TemplateField HeaderText="SI NO" HeaderStyle-HorizontalAlign="center"
                                ItemStyle-HorizontalAlign="center" ItemStyle-Width="3%">
                                <ItemTemplate>
                                    <%# Container.DataItemIndex + 1%>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:BoundField HeaderText="Sub Category" DataField="SUB_CAT" ItemStyle-CssClass="subcat" ItemStyle-Width="15%"
                                HeaderStyle-HorizontalAlign="center" ItemStyle-HorizontalAlign="center" />
                            <asp:BoundField HeaderText="Fees Paid (Months,Quarters)" DataField="PAID_FEES" ItemStyle-Width="15%"
                                HeaderStyle-HorizontalAlign="center" ItemStyle-HorizontalAlign="center" />
                                <asp:BoundField HeaderText="Pending Fees" DataField="PEND_FEES" ItemStyle-Width="5%"
                                HeaderStyle-HorizontalAlign="center" ItemStyle-HorizontalAlign="center" />
                            <asp:TemplateField HeaderText="Current Payment" HeaderStyle-HorizontalAlign="center" ItemStyle-HorizontalAlign="center"
                                ItemStyle-Width="10%">
                                <ItemTemplate>
                                    <asp:TextBox ID="txtmonths" Width="80%" runat="server" 
                                     CssClass="Textbox"></asp:TextBox>
                                    <a href="#" class="openModalLink">
                                        <img style="vertical-align: middle; border: none" width="9%" alt="" 
                                        src="../Images/ico_map.gif"  id="imgmap" class="zoom"/></a>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                </td>
            </tr>
</table>
    <div id="overlay" class="web_dialog_overlay">
        </div>
        <div id="dialog" class="web_dialog">
            <table style="width: 100%; border: 0px;" cellpadding="3" cellspacing="0">
                <tr>
                    <td class="web_dialog_title" style="height: 16px">
                        Month Chooser</td>
                        <td class="web_dialog_title align_right" style="height: 16px">
                        <a href="#" id="btnClose">Close</a>
                    </td>
                </tr>
                <tr>
                <td colspan="2">
                <asp:GridView ID="grdpopup" runat="server" TabIndex="5">
                        <Columns>
                        <asp:TemplateField HeaderText="Select" HeaderStyle-HorizontalAlign="center"
                                ItemStyle-HorizontalAlign="center" ItemStyle-Width="2%">
                                <ItemTemplate>
                                    <asp:CheckBox ID="chkselect" runat="server" CssClass="checkbox" 
                                    Width="15px" Checked="false" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:BoundField HeaderText="Term" DataField="TERM" ItemStyle-Width="35%" HeaderStyle-HorizontalAlign="center" ItemStyle-HorizontalAlign="Left" ItemStyle-CssClass="term" />
                            <asp:BoundField HeaderText="Fee Amt." DataField="FEE_AMT" ItemStyle-Width="35%" HeaderStyle-HorizontalAlign="center" ItemStyle-HorizontalAlign="center" ItemStyle-CssClass="feeamt" />

                        </Columns>
                    </asp:GridView>
                </td></tr>
                <tr>
                    <td rowspan="2" colspan="2" style="text-align: center; vertical-align:middle">
                        <input id="btnSubmit1" type="button" value="Submit" class="button" />
                    </td>
                </tr>
                </table>
                </div>

</div>

JS代码是

<script src="../Jquery Autocomplete/jquery-1.7.2.min.js" type="text/javascript"> 
</script>
<script src="../Jquery Autocomplete/jquery.json-2.2.min.js" type="text/javascript"> 
</script>
    <script type="text/javascript">
$(document).ready(function() 
    {
        var textmonth;
        var textamount;

        $(".openModalLink").click(function()
        {
            textmonth = $(this).parent().find('input[id$=txtmonths]').attr("id");
            textamount = $(this).parent().parent().find('input[id$=txtAmount]').attr("id");
            ShowDialog();
        });
        $("#btnClose").click(function(e) 
        {
            textmonth = null;
            textamount = null;
            HideDialog();
        });
        $('#btnSubmit1').click(function(e) 
        {
            var month = null;
            var amount = 0;
            $(':checkbox:checked').each(function(i) 
            {

                var amt=$(this).closest("tr").find(".feeamt").text();
                if(amt != '')
                {
                    amount = amount + parseInt(amt,10);
                    if(month == null)
                    {
                        month = $(this).closest("tr").find(".term").text();
                    }
                    else
                    {
                        month = month + ',' + $(this).closest("tr").find(".term").text();
                    }
                }
            });
            $("#"+textamount).val(amount);
            $("#"+textmonth).val(month);
            HideDialog();
        });
    });
    function ShowDialog(modal)
    {
        $("#overlay").show();
        $("#dialog").fadeIn(300);
        if (modal) {
            $("#overlay").unbind("click");
        }
    }
    function HideDialog() 
    {
        $("#overlay").hide();
        $("#dialog").fadeOut(300);

    }
    </script>

4 个答案:

答案 0 :(得分:2)

$(this).parent().find('input[id$=' + txtmonths +']').val('foo');

其中txtmonths是所需id的结尾。

Live DEMO

答案 1 :(得分:1)

您需要创建一个选择器并通过它获取元素:

var textmonth = $('#'+$(this).parent().find('input[id$=txtmonths]').attr("id"));

答案 2 :(得分:1)

  1. 首先获取元素ID
  2. 然后通过查询ID选择器

    将该ID与Jquery Selector一起使用
    var textmonth = $(this).parent().find('input[id$=txtmonths]').attr("id");
    
    $('#'+ textmonth).val('your value');
    

答案 3 :(得分:0)

像这样使用

var textmonth = $(this).parent().find('input[id$=txtmonths]')
 remove .attr("id")

然后你可以使用

textmonth.val("only numbers")