【问题标题】:Why RadioButton can fire my Ajax function为什么 RadioButton 可以触发我的 Ajax 函数
【发布时间】:2012-06-14 08:58:05
【问题描述】:

当我更改组中的选择时,我试图让 ajax 函数运行。 但我不知道哪个属性会触发 Ajax。 我试图做各种各样的事情,但我所做的一切都会在浏览器中产生错误。

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Checkout page</title>
<script type="text/javascript">
    var request;
    function NIS2USD() {
        var from = document.getElementById("NIS").value;
        var to = document.getElementById("USD").value;
        var amount = document.getElementById("totalAmountLabel").value;
        request = new XMLHttpRequest();
        request.onreadystatechange = ProcessResponse;
        request.open("GET", "Convert.aspx?from=" + num1 + "&to=" + num2 + "&amount=" +    amount, true);
        request.send();
    }
    function USD2NIS() {
        var from = document.getElementById("USD").value;
        var to = document.getElementById("NIS").value;
        var amount = document.getElementById("totalAmountLabel").value;
        request = new XMLHttpRequest();
        request.onreadystatechange = ProcessResponse;
        request.open("GET", "Convert.aspx?from=" + num1 + "&to=" + num2 + "&amount=" + amount, true);
        request.send();
    }
    function ProcessResponse() {
        if (request.readyState == 4 && request.status == 200) {
            document.getElementById("totalAmountLabel").innerHTML = request.responseText;
        }
    }
</script>
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<form id="form1" runat="server">
<div class="content">
    <div id="top">
        <div class="topright">
            <a href="#"><img src="images/sitemap.png" alt="Sitemap"</a> 
            <a href="#"><img src="images/rss.png" alt="RSS" /></a>              
        </div>
    </div>
    <div id="header">
        <div class="headings">
            <h1>WebBookStore</h1>
            <h2>pay less read more</h2>
        </div>
    </div>
            <div id="menu">
            <ul>
                <li><a href="Login.aspx">Login</a></li>
                    <li><a href="About.aspx">About Us</a></li>
                                <li><a href="Books.aspx">The Books Collection</a></li>
                                <li><a href="SearchBooks.aspx">Search Books</a></li>
                                <li><a href="Cart.aspx">Your Cart</a></li>
                                <li><a href="#">Checkout</a></li>
                    </ul>
          </div>
                  <div id="main">
        <div class="left">
            <asp:Label ID="Label1" runat="server" Font-Size="X-Large" Text="Checkout"</asp:Label>
            <br />
            <br />
            <asp:Label ID="Label2" runat="server" Text="Client ID:"></asp:Label>
            <asp:Label ID="clientIDLabel" runat="server" Text="Label"></asp:Label>
            <br />
            <br />
            <asp:Label ID="Label3" runat="server" Text="Delivery Date:"></asp:Label>
            <asp:Label ID="orderDateLabel" runat="server" Text="Label"></asp:Label>
            <br />
            <br />
            <asp:Label ID="Label4" runat="server" Text="Total:"></asp:Label>
            <asp:Label ID="totalAmountLabel" runat="server" Text="Label"></asp:Label>
            <br />
            <br />
            <asp:Label ID="Label7" runat="server" Text="Currency:"></asp:Label>
            <asp:RadioButton ID="NIS" runat="server"  Checked="True" 
                GroupName='group1' Text="NIS"  />
            <asp:RadioButton ID="USD" runat="server" GroupName='group1' Text="USD" />
            <br />
            <br />
            <asp:Label ID="Label6" runat="server" 
        Text="Pick a delivery date"></asp:Label>
            <br />
            <br />
            <asp:Calendar ID="Calendar1" runat="server" BackColor="White" 
                BorderColor="Black" BorderStyle="Solid" CellSpacing="1" Font-Names="Verdana" 
                Font-Size="9pt" ForeColor="Black" Height="64px" NextPrevFormat="ShortMonth" 
                Width="218px" onselectionchanged="Calendar1_SelectionChanged">
                <DayHeaderStyle Font-Bold="True" Font-Size="8pt" ForeColor="#333333" 
                    Height="8pt" />
                <DayStyle BackColor="#CCCCCC" />
                <NextPrevStyle Font-Bold="True" Font-Size="8pt" ForeColor="White" />
                <OtherMonthDayStyle ForeColor="#999999" />
                <SelectedDayStyle BackColor="#333399" ForeColor="White" />
                <TodayDayStyle BackColor="#999999" ForeColor="White" />
            </asp:Calendar>
            <br />
            <asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
                Text="Submit Order" />  
            <br />
            <asp:TextBox ID="TextBox1" runat="server" ForeColor="Red" Height="67px" 
                ReadOnly="True" TextMode="MultiLine" Visible="False" Width="451px"></asp:TextBox>
            <br />
        </div>
    </div>
    <div id="footer">
        <div class="info">
            &copy; 2012 WebBookStore<br />
            Site Design - <a href="http://www.URL.com">WebBookStore</a>
        </div>
     </div>
    </div>
</form>

【问题讨论】:

    标签: asp.net ajax html radio-button


    【解决方案1】:

    我会将您的单选按钮包装在一个 div 中并像这样访问它们。

    <script type="text/javascript">
    
        $(function () {
            $("#rbs input").on("click", function() {
                DoSomthing($(this).attr("value"));
            });
        });
    
    
        function DoSomthing(val) {
            $("#<%=totalAmountLabel.ClientID%>").html(val);
        }
    
    </script>
    
    <div id="rbs">
        <p>
            <asp:RadioButton ID="NIS" runat="server" Checked="True" GroupName="rbGroup" Text="NIS"/>
            <asp:RadioButton ID="USD" runat="server" GroupName="rbGroup" Text="USD"/>
    
    </div>
    <asp:Label ID="totalAmountLabel" runat="server" Text="Label"></asp:Label>
    

    希望对你有帮助

    编辑:添加了如何使用 jquery Selector 获取 ASP.NET 控件。

    【讨论】:

    • 感谢您的帮助!其作品!!!但我有一个不同的问题知道,我无法在我的方案中获得标签的值我有&lt;asp:Label ID="totalAmountLabel" runat="server" Text="Label"&gt;&lt;/asp:Label&gt; 你可以在我的 AJAX 上看到我正在尝试阅读它,但它把 undefind 放在var amout :( 有什么想法吗?
    • 我已经编辑了上面的解决方案。请标记为已回答。非常感谢
    【解决方案2】:

    此代码用于 c#,您可以在 page_load() 中添加

    NIS.Attributes.Add("onclick", "USD2NIS();");
    USD.Attributes.Add("onclick", "NIS2USD();");
    

    【讨论】:

      猜你喜欢
      • 2019-01-16
      • 1970-01-01
      • 2020-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-06
      • 1970-01-01
      相关资源
      最近更新 更多